Questions tagged [bit-fields]

A bit field is used to compactly store multiple logical values as a short series of bits where each of the single bits can be addressed separately.

A bit field is used to represent and store a set of known width and logically grouped set of values. These fields can then be addressed individually in the code. A common use of such a construct is flags.

In language such as C and C++, bit fields can also be used to abstract and interop with specific hardware.

857 questions
21
votes
6 answers

Integer to bitfield as a list

I've created a method to convert an int to a bitfield (in a list) and it works, but I'm sure there is more elegant solution- I've just been staring at it for to long. I'm curious, how would you convert a int to a bitfield represented in a list? def…
tMC
  • 18,105
  • 14
  • 62
  • 98
20
votes
4 answers

Overflow in bit fields

can I trust that the C compiler does modulo 2^n each time I access a bit field? Or is there any compiler/optimisation where a code like the one below would not print out Overflow? struct { uint8_t foo:2; } G; G.foo = 3; G.foo++; if(G.foo == 0)…
Florian
  • 203
  • 2
  • 5
20
votes
2 answers

Justification for using a bitfield instead of EnumSet in modern Java 8 API

EnumSet, as old as the enum itself (both since Java 5), is supposed to be a noncompromizing replacement for the use case of bitfields: as fast and lean as the bitfield (well, except for not being a primitive type), and typesafe to boot. On the other…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
20
votes
7 answers

What is the type of a bitfield?

I can't find anywhere in the C standard where this is specified. For example, in struct { signed int x:1; } foo; is foo.x an lvalue of type int, or something else? It seems unnatural for it to be an lvalue of type int since you cannot store any…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
19
votes
5 answers

Bit field vs Bitset

I want to store bits in an array (like structure). So I can follow either of the following two approaches Approach number 1 (AN 1) struct BIT { int data : 1 }; int main() { BIT a[100]; return 0; } Approach number 2 (AN 2) int main() { …
CLOWN
  • 193
  • 1
  • 1
  • 4
19
votes
6 answers

Size of a bitfield member?

Would anyone know how to extract the size of a bit-field member. The below code naturally gives me the size of an integer, but how do I find out how many bits or bytes are in mybits.one? I've tried sizeof(test.one) but which clearly won't work. I…
domonica
  • 526
  • 7
  • 14
19
votes
3 answers

Is it valid to use bit fields with union?

I have used bit field with a structure like this, struct { unsigned int is_static: 1; unsigned int is_extern: 1; unsigned int is_auto: 1; } flags; Now i wondered to see if this can be done with a union so i modified the code…
amin__
  • 1,018
  • 3
  • 15
  • 22
18
votes
5 answers

Can Microsoft store three-valued fields in a single bit?

I'm completely ignorant of SQL/databases, but I was chatting with a friend who does a lot of database work about how some databases use a "boolean" field that can take a value of NULL in addition to true and false. Regarding this, he made a comment…
fenomas
  • 11,131
  • 2
  • 33
  • 57
17
votes
2 answers

Bit-fields and sequence points

For an implementation that packs f0 and f1 into the same byte, is the program below defined? struct S0 { unsigned f0:4; signed f1:4; } l_62; int main (void) { (l_62.f0 = 0) + (l_62.f1 = 0); return 0; } I am interested…
Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
17
votes
4 answers

Extension method for adding value to bit field (flags enum)

Instead of doing this to add a value to flags enum variable: MyFlags flags = MyFlags.Pepsi; flags = flags | MyFlags.Coke; I'd like to create an extension method to make this possible: MyFlags flags =…
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
17
votes
4 answers

struct bitfield max size (C99, C++)

What is maximal bit width for bit struct field? struct i { long long i:127;} Can I define a bit field inside struct, with size of bitfield up to 128 bit, or 256 bit, or larger? There are some extra-wide vector types, like sse2 (128-bit), avx1/avx2…
osgx
  • 90,338
  • 53
  • 357
  • 513
16
votes
3 answers

What does an unnamed zero length bit-field mean in C?

I saw the following example in the C standard draft (n1570): $3.14 paragraph 4 : A structure declared as: struct { char a; int b:5, c:11, :0, d:8; struct { int ee:8; } e; } So, What does :0…
msc
  • 33,420
  • 29
  • 119
  • 214
16
votes
3 answers

Which end of a bit field is the most significant bit?

I'm writing a C++ application for Windows XP/Vista/7 using Visual Studio 2008. Some of my structures use a bit field, as shown in the example. typedef struct myStruct_tag { BYTE myVar1; WORD myVar2; WORD myVar3; union { …
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
16
votes
4 answers

Converting Bit Field to int

I have bit field declared this way: typedef struct morder { unsigned int targetRegister : 3; unsigned int targetMethodOfAddressing : 3; unsigned int originRegister : 3; unsigned int originMethodOfAddressing : 3; unsigned int…
Shahar Gvirtz
  • 2,418
  • 1
  • 14
  • 17
16
votes
2 answers

Why are non-const references to bitfields prohibited?

Section 9.6/3 in C++11 is unusually clear: "A non-const reference shall not be bound to a bit-field." What is the motivation behind this prohibition? I understand that it's not possible to directly bind a reference to a bitfield. But if I declare…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
1 2
3
57 58