Bit-flag Confusion

Aug 6, 2011 at 4:05pm
closed account (zb0S216C)
Firstly, here's my code:

1
2
3
4
5
6
7
8
9
typedef short HOLOBYTE, DOUBLEBYTE;

typedef enum T_ENUM_HOLOBYTE
{
    LOBYTE_CALL_FAILED = 3,   // 0011 0000
    LOBYTE_CALL_OK = 0C,      // 1100 0000
    HOBYTE_ALLOC_FAILED = ??, // 0000 ????
    HOBYTE_ALLOC_OK = ??      // 0000 ????
} ENUM_HOLOBYTE;

Here, my target is to fill 2 bytes with 4 flags (2 flags per byte). However, I cannot get my head around defining flags for the HOBYTE. What could the values be? Note that I cannot explain this clearly, so I'm sorry if I've confused you :)

Wazzak
Aug 6, 2011 at 4:16pm
You want a single flag to take two bits?

Use your computer's calculator to convert between binary and octal, or whatever notation you want to use in your code.

1
2
3
4
    LOBYTE_CALL_FAILED  = 0x03,  // 0000 0011
    LOBYTE_CALL_OK      = 0x0C,  // 0000 1100
    HOBYTE_ALLOC_FAILED = 0x30,  // 0011 0000
    HOBYTE_ALLOC_OK     = 0xC0   // 1100 0000 

Unless you have to have specific bits selected for your bit flags, you can just use a simple trick to specify them:

1
2
3
4
    LOBYTE_CALL_FAILED  = 1 << 0,  // 0000 0001
    LOBYTE_CALL_OK      = 1 << 1,  // 0000 0010
    HOBYTE_ALLOC_FAILED = 1 << 2,  // 0000 0100
    HOBYTE_ALLOC_OK     = 1 << 3   // 0000 1000 

Hope this helps.
Aug 6, 2011 at 4:30pm
closed account (zb0S216C)
Thanks, Duoas :) Your first code block gave me what I needed. If you lived near by, I would've given you half of my jam sandwich.

Thanks again :)

Wazzak
Aug 6, 2011 at 9:00pm
I could move. :-)
Topic archived. No new replies allowed.