bit-fields

Struct - Explain the output:

折月煮酒 提交于 2019-12-01 22:55:59
问题 I have the following C code. struct values{ int a:3; int b:3; int c:2; }; void main(){ struct values v={2,-6,5}; printf("%d %d %d",v.a,v.b,v.c); } When I execute the code, I am getting the following output: 2 2 1. But output should be 2 -6 5 , right? If I'm wrong please explain. 回答1: -6 exceeds the range of a 3-bit signed int. Therefore you're observing an artifact of undefined implementation-defined behaviour (in practice, the most-significant bits of your value are being thrown away). 回答2:

How to use bit field with Swift to store values with more than 1 bit

岁酱吖の 提交于 2019-12-01 18:57:40
In C I can do something like this: struct byte_nibbles { unsigned char b1: 4; unsigned char b2: 4; unsigned char b3: 4; unsigned char b4: 4; unsigned char b5: 4; unsigned char b6: 4; unsigned char b7: 4; unsigned char b8: 4; }; union { unsigned long var; struct byte_nibbles b; } u; int main(void) { u.b.b1=0x01; u.b.b2=0x02; u.b.b3=0x03; u.b.b4=0x04; u.b.b5=0x05; u.b.b6=0x06; u.b.b7=0x07; u.b.b8=0x08; return 0; } So I can access specific parts of the byte_nibbles. Obviously this is just one example. It is possible to create bit fields of any size that fits in the basic types. Despite my efforts

How slow are bit fields in C++

こ雲淡風輕ζ 提交于 2019-12-01 16:27:19
I have a C++ application that includes a number of structures with manually controlled bit fields, something like #define FLAG1 0x0001 #define FLAG2 0x0002 #define FLAG3 0x0004 class MyClass { ' ' unsigned Flags; int IsFlag1Set() { return Flags & FLAG1; } void SetFlag1Set() { Flags |= FLAG1; } void ResetFlag1() { Flags &= 0xffffffff ^ FLAG1; } ' ' }; For obvious reasons I'd like to change this to use bit fields, something like class MyClass { ' ' struct Flags { unsigned Flag1:1; unsigned Flag2:1; unsigned Flag3:1; }; ' ' }; The one concern I have with making this switch is that I've come

Bitfields, why implementation specific?

心不动则不痛 提交于 2019-12-01 15:23:12
C/C++ bitfields seem to have a lot of application in hardware drivers and binary network transfers. However they don't seem to be widely used and are generally discouraged, because the actual binary layout is implementation specific, as seen in this quote from the C99 standard 6.7.2.1/10 - "Structure and union specifiers"; An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains,

Bitfields, why implementation specific?

荒凉一梦 提交于 2019-12-01 14:21:35
问题 C/C++ bitfields seem to have a lot of application in hardware drivers and binary network transfers. However they don't seem to be widely used and are generally discouraged, because the actual binary layout is implementation specific, as seen in this quote from the C99 standard 6.7.2.1/10 - "Structure and union specifiers"; An implementation may allocate any addressable storage unit large enough to hold a bitfield. If enough space remains, a bit-field that immediately follows another bit-field

Assigning a value to bitfield with length 1

烈酒焚心 提交于 2019-12-01 12:34:05
Suppose I have struct A { signed char a:1; unsigned char b:1; }; If I have A two, three; two.a = 2; two.b = 2; three.a = 3; three.b = 3; two will contain 0 s in its fields, while three will contain 1 s. So, this makes me think, that assigning a number to a single-bit-field gets the least significant bit ( 2 is 10 in binary and 3 is 11 ). So, my question is - is this correct and cross-platform? Or it depends on the machine, on the compiler, etc. Does the standard says anything about this, or it's completely implementation defined? Note: The same result may be achieved by assigning 0 and 1 ,

Assigning a value to bitfield with length 1

你。 提交于 2019-12-01 11:02:31
问题 Suppose I have struct A { signed char a:1; unsigned char b:1; }; If I have A two, three; two.a = 2; two.b = 2; three.a = 3; three.b = 3; two will contain 0 s in its fields, while three will contain 1 s. So, this makes me think, that assigning a number to a single-bit-field gets the least significant bit ( 2 is 10 in binary and 3 is 11 ). So, my question is - is this correct and cross-platform? Or it depends on the machine, on the compiler, etc. Does the standard says anything about this, or

What's the purpose of unnamed bit field at the end of structure

风流意气都作罢 提交于 2019-12-01 07:10:49
I am learning C. In C Primer Plus , I saw an bit field example as follows: struct box_props { bool opaque : 1; unsigned int fill_color : 3; unsigned int : 4; bool show_border : 1; unsigned int border_color : 3; unsigned int border_style : 2; unsigned int : 2; }; I do understand the 4-bit unnamed bit field in the middle is used for letting the following bits start at a new byte. However, I don't understand why there is another unnamed bit field at the end of the structure. What's the purpose of it? Is it necessary? Is it necessary? Nope, it's optional. What's the purpose of it? Here's what the

size of a structure containing bit fields [duplicate]

无人久伴 提交于 2019-12-01 06:34:45
Possible Duplicate: Why isn't sizeof for a struct equal to the sum of sizeof of each member? I was trying to understand the concept of bit fields. But I am not able to find why the size of the following structure in CASE III is coming out as 8 bytes. CASE I: struct B { unsigned char c; // +8 bits } b; sizeof(b); // Output: 1 (because unsigned char takes 1 byte on my system) CASE II: struct B { unsigned b: 1; } b; sizeof(b); // Output: 4 (because unsigned takes 4 bytes on my system) CASE III: struct B { unsigned char c; // +8 bits unsigned b: 1; // +1 bit } b; sizeof(b); // Output: 8 I don't

Implementing a C style bitfield in Java

孤人 提交于 2019-12-01 05:19:07
I have a problem that I am a bit stuck on and I was informed by a colleague that this would be a good place to seek help. I am trying to implement a C style bitfield in Java. Here is a rough example (I do not have the actual code in front of me at this moment). typedef union { typedef struct { unsigned short a :1; unsigned short b :1; unsigned short c :2; unsigned short d :10; } bitfield; unsigned short bitmap; }example_bitfield; I have a good bit of similar style bitfields from legacy code. The reason that I need to come up with an equivalent method for Java is that I am working on code that