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
38
votes
15 answers

What is the most efficient way to represent small values in a struct?

Often I find myself having to represent a structure that consists of very small values. For example, Foo has 4 values, a, b, c, d that, range from 0 to 3. Usually I don't care, but sometimes, those structures are used in a tight loop; their values…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
34
votes
4 answers

Is there a bit-equivalent of sizeof() in C?

Sizeof() doesn't work when applied to bitfields: # cat p.c #include int main( int argc, char **argv ) { struct { unsigned int bitfield : 3; } s; fprintf( stdout, "size=%d\n", sizeof(s.bitfield) ); } # gcc p.c -o p p.c: In…
eruciform
  • 7,680
  • 1
  • 35
  • 47
33
votes
4 answers

Is it safe to use an enum in a bit field?

Say, I've got the following struct: typedef struct my_struct{ unsigned long a; unsigned long b; char* c; unsigned int d1 :1; unsigned int d2 :4; unsigned int d3 :4; …
eckes
  • 64,417
  • 29
  • 168
  • 201
32
votes
4 answers

Colons after variable name in C

Possible Duplicate: What does a colon in a struct declaration mean, such as :1, :7, :16, or :32? This is C code sample of a reference page. signed int _exponent:8; What's the meaning of the colon before '8' and '8' itself?
eonil
  • 83,476
  • 81
  • 317
  • 516
31
votes
8 answers

Using Bitwise operators on flags

I have four flags Current = 0x1 Past = 0x2 Future = 0x4 All = 0x7 Say I receive the two flags Past and Future (setFlags(PAST | FUTURE)). How can I tell if Past is in it? Likewise how can I tell that Current is not in it? That way I don't…
Malfist
  • 31,179
  • 61
  • 182
  • 269
31
votes
4 answers

Is it possible to use array of bit fields?

I am curious to know, Is it possible to use array of bit fields? Like: struct st { unsigned int i[5]: 4; };
msc
  • 33,420
  • 29
  • 119
  • 214
28
votes
4 answers

Why class size increases when int64_t changes to int32_t

In my first example I have two bitfields using int64_t. When I compile and get the size of the class I get 8. class Test { int64_t first : 40; int64_t second : 24; }; int main() { std::cout << sizeof(Test); // 8 } But when I change…
xinaiz
  • 7,744
  • 6
  • 34
  • 78
26
votes
2 answers

What is the best way to do Bit Field manipulation in Python?

I'm reading some MPEG Transport Stream protocol over UDP and it has some funky bitfields in it (length 13 for example). I'm using the "struct" library to do the broad unpacking, but is there a simple way to say "Grab the next 13 bits" rather than…
ZebZiggle
  • 678
  • 2
  • 9
  • 11
26
votes
4 answers

How to simulate bit-fields in Delphi records?

I would like to declare a record in Delphi that contains the same layout as it has in C. For those interested : This record is part of a union in the Windows OS's LDT_ENTRY record. (I need to use this record in Delphi because I'm working on an Xbox…
PatrickvL
  • 4,104
  • 2
  • 29
  • 45
26
votes
2 answers

Packed bit fields in c structures - GCC

I am working with structs in c on linux. I started using bit fields and the "packed" attribute and I came across a wierd behavior: struct __attribute__((packed)) { int a:12; int b:32; int c:4; } t1; struct __attribute__((packed)) { …
Danny Cohen
  • 505
  • 1
  • 5
  • 11
25
votes
4 answers

gcc suppress warning "too small to hold all values of"

I need to use scoped enums so that I can pass them as specific types to our serialiser. I have given explicit integer values for the enum members of Enum1. I have put two scoped enums matching the description above into a bitfield thus enum class…
jsren
  • 325
  • 3
  • 7
25
votes
6 answers

Is there a portable alternative to C++ bitfields

There are many situations (especially in low-level programming), where the binary layout of the data is important. For example: hardware/driver manipulation, network protocols, etc. In C++ I can read/write arbitrary binary structures using char* and…
Antoine
  • 13,494
  • 6
  • 40
  • 52
23
votes
4 answers

How to use binary flags in Core Data?

I have an int32 attribute in a Core Data database. I use this int as an enum bit field. Is it possible to create a NSPredicate to query items based on the binary value of this int ? Something like @"bitFieldAttribute & 0x0001"? I'm also wondering if…
CodeFlakes
  • 3,671
  • 3
  • 25
  • 28
22
votes
4 answers

Are there reasons to avoid bit-field structure members?

I long knew there are bit-fields in C and occasionally I use them for defining densely packed structs: typedef struct Message_s { unsigned int flag : 1; unsigned int channel : 4; unsigned int signal : 11; } Message; When I read open…
wirrbel
  • 3,173
  • 3
  • 26
  • 49
22
votes
5 answers

c - cannot take address of bit-field

Why cannot take address of bit-field? How do I make a pointer to bit-field? Here is the code... struct bitfield { unsigned int a: 1; unsigned int b: 1; unsigned int c: 1; unsigned int d: 1; }; int main(void) { struct bitfield…
sleepy_dog
  • 277
  • 1
  • 2
  • 11
1
2
3
57 58