This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does 'unsigned temp:3' means
I encountered a problem when I'm reading the code of Clang.
class LangOptions {
public:
unsigned Trigraphs : 1; // Trigraphs in source files.
unsigned BCPLComment : 1; // BCPL-style '//' comments.
...
};
This is the first time I saw the syntax " : 1", what's the " : 1" stands for? Thanks!
It's a bitfield, which means that the value will only use one bit, instead of 32 (or whatever sizeof(unsigned) * <bits-per-byte> is on your platform).
Bitfields are useful for writing compact binary data structures, although they come with some performance cost as the compiler/CPU cannot update a single bit, but needs to perform AND/OR operations when reading/writing a full byte.
Trigraphs and BCPLComment use 1 bit only for saving values.
For example,
struct S
{
signed char type : 2;
signed char num : 4;
signed char temp : 2;
};
uses only 8 bit of memory. struct S may use a single byte or memory.
sizeof(S) is 1 for the case of some implementations.
But type and temp is equal to 0,1,2 or 3. And num is
equal 0,1,2,..., 15 only.
These are bit fields. The "1" is the width in bits.
See the C FAQ for an explanation.
Related
This question already has answers here:
Is it possible to create a type in c++ that takes less than one byte of memory?
(5 answers)
Closed 5 years ago.
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};
This question already has answers here:
What does C++ struct syntax "a : b" mean
(5 answers)
":" (colon) in C struct - what does it mean? [duplicate]
(3 answers)
When to use bit-fields in C
(16 answers)
Closed 5 years ago.
This was a code snippet from a test question. The question was what the size of S would be.
struct S
{
char a : 4;
unsigned char b : 3;
signed char : 2;
char c : 1;
char d : 5;
};
What does the ":" do? Is there any difference when it is applied to a signed or unsigned char (or any other data type)? When is this usually used?
It's a bit field, it says that for example char a will only have 4 bits of memory instead of normal 8. Unsigned char b will have only 3 bits of memory. Number of bits limits the range of values it can hold.
Bit Field declares a class data member with explicit size, in bits. Adjacent bit field members may be packed to share and straddle the individual bytes.
http://en.cppreference.com/w/cpp/language/bit_field
This question already has answers here:
C++ : why bool is 8 bits long?
(7 answers)
Closed 9 years ago.
For bool, it's 8 bit while has only true and false, why don't they make it single bit.
And I know there's bitset, however it's not that convenient, and I just wonder why?
The basic data structure at the hardware level of mainstream CPUs is a byte. Operating on bits in these CPUs require additional processing, i.e. some CPU time.
The same holds for bitset.
Not exactly an answer to why there is not a native type. But you can get a 1-bit type inside of a struct like this:
struct A {
int a : 1; // 1 bit wide
int b : 1;
int c : 2; // 2 bits
int d : 4; // 4 bits
};
Thus, sizeof(A) == 1 could be if there wouldn't be the padding (which probably takes it to a multiple of sizeof(void*), i.e. maybe 4 for 32bit systems).
Note that you cannot get a pointer to any of these fields because of the reasons stated by the other people. That might also be why there does not exist a native type.
This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Why does a struct consisting of a char, short, and char (in that order), when compiled in C++ with 4-byte packing enabled, come to a 6-byte struct?
(3 answers)
Closed 9 years ago.
So I am currently a student and am having a programming couse.
Today we had about the use of sizeof in different classes(if it had 1 int or 2 int´s and so on)
One part of the example I found weird was this:
class TwoIntAndACharClass
{
public:
int x_;
int y_;
char z_;
};
and the part to test it
TwoIntAndACharClass o3b;
cout << "Sizeof TwoIntAndACharClass = " << sizeof(o3b) << "\n";
So in the program i could see a class with 1 char took 1 byte. So when I saw this I thought I would see 9 bytes and not 12
So first I thought that it was weird but after some time I came to the conclusion that it might save some kind of blocks of 4 bytes.
To be 100% sure that this was true I then tried adding a new variable into the class(a double variable of 8 bytes) and the total size increased from 12 bytes to now 24 bytes. That ment that the char now had to be 8 bytes long so my last theory failed.
My last theory was that it would take the biggest already declared variable and use the size of that for the char variable _z , as this worked with both long long int(8 bytes) and a double(also 8 bytes)
So my question is, is my last theory true - or is it something different making the char take more memory then needed? (my teacher did say that each compiler could handle this differently but I have tried it on microsoft visual studio and a friend tried it on another compiler with the same results, but is that really true, is it the way the compiler handle this?)
Sorry for my poor english.
sizeof gives you the size of the struct, not the sum of the sizes of its members. Due to alignment requirements (ints generally like to be aligned on natural boundaries; 4 bytes on most platforms), the compiler will pad the struct to 12 bytes, so that when you have multiple instances of the struct adjacent to each other (e.g. in an array), they stay correctly aligned.
Most compilers have some custom extension to control padding, e.g. #pragma pack in Microsoft's compiler.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does 'unsigned temp:3' means
what does this mean in c int a:16;?
I came across this struct in some C++ code I'm working on. Can someone explain to me what the colon operator is doing and why one would use it?
struct MYMSG
{
unsigned short src : 4;
unsigned short dst : 11;
unsigned short tx : 1;
};
As commented above, it's the number of bits to be used for each field.
struct MYMSG
{
unsigned short src : 4; // allows values 0 - 15
unsigned short dst : 11; // allows values 0 - 2047
unsigned short tx : 1; // allows values 0 - 1
};
This also has the effect of packing the structure if alignment is turned off. If this structure is not padded, then a call to sizeof() will return 2 (on an 8-bit/byte architecture).
In this case, a single unsigned short is allocated, and the bit fields are divided up within that value. Setting a value outside the range of one of the fields (such as 16 for src) will cause an overflow of that particular field, but will not alter the values of any other fields (the value of dst will not change).
Take a slightly more obtuse example:
struct T
{
unsigned long val : 4
};
This still allocates a full unsigned long (32bit on most architectures), but only allows for setting of the first 4 bits giving valid values of 0-15.