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
Related
This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 5 years ago.
Why is this code:
#include <iostream>
struct S {
unsigned char a:6, b:6, c:6, d:6;
};
int main(int argc, char *argv[]) {
std::cout << sizeof(S);
return 0;
}
returning 4? Shouldn't it have a size of 4 x 6 = 24b = 3B? In contrast, this code:
struct S { unsigned char a:4, b:4, c:4, d:4; };
returns a 2, while this one:
struct S { unsigned char a:4, b:4, c:4, d:4, e:4, f:4; };
returns a 3...
From CppReference: Bit Fields:
The following properties of bit fields are implementation-defined:
[...]
Everything about the actual allocation details of bit fields within the class object
For example, on some platforms, bit fields don't straddle bytes, on others they do
So yes, it's implementation-defined (i.e., up to the compiler).
So, even though the compiler calculates the other 2 without padding, it adds padding the first one?
Yes, because in the first case, values would "straddle" byte borders if not padded, while in the other two examples, they don't.
Again, this is implementation-defined and could be handled differently on another platform, a different compiler on the same platform, or even a different version of the same compiler.
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:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Padding in structures in C
(5 answers)
Closed 6 years ago.
With the below code:
struct S {
unsigned char b1 : 3;
int i;
};
I can deduce that char b1 is of 1 byte and using 3 bits of that and int is 4 bytes but when I try to print out the size of S, I get it 8 bytes instead of 5 bytes(1 for char+4 for int) as I expected and this is something I am not understanding by obvious.
Can something explain why size of S is 8 bytes?
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.
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.