bit field question
-
I don't use bit fields much at all. I have a simple struct as folows (on an INTEL format machine): typedef struct{ short f1 : 13; short f2 : 1; short f3 : 1; short f4 : 1; } tststruct; tststruct tst; tst.f1 = 8000; what ends up happening is f1 gets a value of 0xFF40 instead of what I thought I would get (0x1F40). The other members (f2,f3,f4) are all 0x00 as I would expect. Any idea why?
-
I don't use bit fields much at all. I have a simple struct as folows (on an INTEL format machine): typedef struct{ short f1 : 13; short f2 : 1; short f3 : 1; short f4 : 1; } tststruct; tststruct tst; tst.f1 = 8000; what ends up happening is f1 gets a value of 0xFF40 instead of what I thought I would get (0x1F40). The other members (f2,f3,f4) are all 0x00 as I would expect. Any idea why?
Maybe the compiler fills up the remaining 3 bits with ones instead of zeros? Maybe you can do an & with 0x1FFF to filter out the first 3 bits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Maybe the compiler fills up the remaining 3 bits with ones instead of zeros? Maybe you can do an & with 0x1FFF to filter out the first 3 bits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Thanks. Here's what I found: if I use unsigned shorts, everything works fine. Apparently, the value is sign- extended when using signed variables - so the 1F40 (when looking at it as a 13 bit number) is a negative number and the sign bit is extended out when placing the value in the struct (giving FF40). Hope that makes sense. Thanks - I got there eventually but it really looked strange at first.