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
147
votes
21 answers

Is it safe to use -1 to set all bits to true?

I've seen this pattern used a lot in C & C++. unsigned int flags = -1; // all bits are true Is this a good portable way to accomplish this? Or is using 0xffffffff or ~0 better?
hyperlogic
  • 7,525
  • 7
  • 39
  • 32
98
votes
6 answers

Why is assigning a value to a bit field not giving the same value back?

I saw the below code in this Quora post: #include struct mystruct { int enabled:1; }; int main() { struct mystruct s; s.enabled = 1; if(s.enabled == 1) printf("Is enabled\n"); // --> we think this to be printed else …
iammilind
  • 68,093
  • 33
  • 169
  • 336
86
votes
12 answers

Bit fields in C#

I have a structure which I need to populate and write to disk (several actually). An example is: byte-6 bit0 - original_or_copy bit1 - copyright bit2 - data_alignment_indicator bit3 - PES_priority bit4-bit5 - PES_scrambling control. …
Rob
  • 4,210
  • 3
  • 30
  • 25
75
votes
7 answers

Why bit endianness is an issue in bitfields?

Any portable code that uses bitfields seems to distinguish between little- and big-endian platforms. See the declaration of struct iphdr in linux kernel for an example of such code. I fail to understand why bit endianness is an issue at all. As far…
Leonid99
  • 1,227
  • 2
  • 12
  • 10
65
votes
4 answers

Storing 8 logical true/false values inside 1 byte?

I'm working on a microcontroller with only 2KB of SRAM and desperately need to conserve some memory. Trying to work out how I can put 8 0/1 values into a single byte using a bitfield but can't quite work it out. struct Bits { int8_t b0:1, b1:1,…
Jazcash
  • 3,145
  • 2
  • 31
  • 46
63
votes
4 answers

What does 'unsigned temp:3' in a struct or union mean?

Possible Duplicate: What does this C++ code mean? I'm trying to map a C structure to Java using JNA. I came across something that I've never seen. The struct definition is as follows: struct op { unsigned op_type:9; //---> what does this…
Munir Ahmed
  • 641
  • 1
  • 5
  • 4
62
votes
3 answers

What does a colon in a struct declaration mean, such as :1, :7, :16, or :32?

What does the following C++ code mean? unsigned char a : 1; unsigned char b : 7; I guess it creates two char a and b, and both of them should be one byte long, but I have no idea what the ": 1" and ": 7" part does.
derrdji
  • 12,661
  • 21
  • 68
  • 78
58
votes
11 answers

When is it worthwhile to use bit fields?

Is it worthwhile using C's bit-field implementation? If so, when is it ever used? I was looking through some emulator code and it looks like the registers for the chips are not being implemented using bit fields. Is this something that is avoided…
Russel
  • 595
  • 1
  • 4
  • 5
56
votes
12 answers

Does Python have a bitfield type?

I need a compact representation of an array of booleans, does Python have a builtin bitfield type or will I need to find an alternate solution?
Gordon Wrigley
  • 11,015
  • 10
  • 48
  • 62
46
votes
3 answers

In C, what does a colon mean inside a declaration?

Possible Duplicate: What does ‘unsigned temp:3’ means I'm learning some kernel code, and came along the following line (in linux 2.4, sched.h, struct mm_struct): unsigned dumpable:1; What does this mean?
Tzafrir
  • 1,801
  • 2
  • 16
  • 18
43
votes
4 answers

How is the size of a struct with Bit Fields determined/measured?

#include typedef struct size { unsigned int a:1; unsigned int b:31; unsigned int c:1; } mystruct; int main() { mystruct a; printf("%d", sizeof(a)); return 0; } With int b:31, the output…
Jagan
  • 4,649
  • 19
  • 60
  • 70
43
votes
6 answers

C++ bitfield packing with bools

I've just done a test with bitfields, and the results are surprising me. class test1 { public: bool test_a:1; bool test_b:1; bool test_c:1; bool test_d:1; bool test_e:1; bool test_f:1; bool test_g:1; bool…
Roddy
  • 66,617
  • 42
  • 165
  • 277
42
votes
14 answers

Declaring and using a bit field enum in Swift

How should bit fields be declared and used in Swift? Declaring an enum like this does work, but trying to OR 2 values together fails to compile: enum MyEnum: Int { case One = 0x01 case Two = 0x02 case Four = 0x04 case…
Pascal Bourque
  • 5,101
  • 2
  • 28
  • 45
39
votes
5 answers

C++11 standard conformant bitmasks using enum class

Can you implement standard conformant (as described in 17.5.2.1.3 of the n3242 draft) type safe bitmasks using enum class? The way I read it, a type T is a bitmask if it supports the |,&,^,~,|=,&= and ^= operators and further you can do if(l&r)…
B.S.
  • 1,435
  • 2
  • 12
  • 18
38
votes
5 answers

Practical Use of Zero-Length Bitfields

I am not totally sure about C, but C++ allows unnamed bit-fields of 0 length. For example: struct X { int : 0; }; Question one: What practical uses of this can you think of? Question two: What real-world practical uses (if any) are you aware…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1
2 3
57 58