Bit Fields

hi friends, I wanna know How bit fields actually work ? how they save memory thanks in advance :)
Bitset allows you to manipulate individual bits in a word.

Let's say that you have 32 options which could be set to true or false in your program. You could either store them in 32 bools, consuming 32 bytes, or store them in a bitset which could consume 4 bytes instead. Your space consumed is reduced by 88%.

Another place this can be used is if we are packing opcodes for specific usage or for network transmission.

Note that I don't really like this class. The fact that it can be resizeable means that it implements dynamic memory allocation which is actually rather slow. When I have to work in real-time at a very low-level and pack individual bits, I can't afford to use the new keyword in any objects which could be used hundreds of times in an iteration. I've made my own classes which ease bit-setting with 32-bit words and these have actually brought down some of my module's execution time from 1ms to 10us (100x faster).
bit fields aren't the same thing as bitset
bitset with 4 bools:
std::bitset<4> set; on typical implementation, its size is 8 bytes (bitsets are designed for long bit strings)

4 one-bit bit fields:
1
2
3
4
5
struct Set {
    bool one : 1, two:1, three:1, four:1;
} set;

assert(sizeof set == 1);

Last edited on
Whoops, mixed up bitset with bit fields.

I do like bit-fields. In the previous example, I talked about how you can pack significant data into a single 32-bit word. Bit-fields let us access this easily:
1
2
3
4
5
6
7
8
struct MyWord
{
    unsigned char ident  : 8
    unsigned char source : 2
    unsigned int  data   : 19
    unsigned char status : 2 
    unsigned char parity : 1
};


The numbers on the right-hand side define how many bits are used for each field. When I write to data, the numbers are automatically shifted for me. I'm not sure about masking though. You may need to protect against overflow (I'm not sure what MyWord w; w.source = 0x4; would do).
I'm not sure what MyWord w; w.source = 0x4; would do)
test.cc:14:28: warning: implicit truncation from 'int' to bitfield changes value from 4 to 0 [-Wbitfield-constant-conversion]
        MyWord w; w.source = 0x4;
                           ^ ~~~
Last edited on
Cool, there is some protection in the compiler. Though I think it would be wise to not rely on that.
bit fields have been around since C89, it's unlikely that a today's compiler would have a bug dealing with unsigned overflow on a bit field (signed overflow, is, as always, UB). And if it does, a bug report would help others.
True, but unions have been around for quite a while too and a lack of protection is why a lot of people advise against using them for holding anything but pointers.
That doesn't really make sense. what "protection" and how does it apply to pointers differently from other types?
I'm talking about setting a 2-bit bit-field to 0x4. Your compiler gave a warning and truncated the 0x4 to fit 2 bits. I would not rely on that truncation as I just went through the C++03 standard and this behaviour appears to be undefined. The fact that you got a warning confirms that it's something we should look at before releasing.

I wouldn't call truncation versus overflow a "bug" as it's undefined.

I'm not going to get into unions because that's getting too off topic.
Last edited on
That truncation (technically, modulo arithmetic) is required by C++03 as well as all other standards for unsigned types, which source was in the example.

The fact that you got a warning confirms that it's something we should look at before releasing.

Yes, but not because the result is not reliable. It's a style warning, like for if(a=b)
Topic archived. No new replies allowed.