Bitfields and printf()

Walter Murray walter at hpclwjm.HP.COM
Sat Feb 17 07:27:03 AEST 1990


Darrin A. Hyrup writes:

>   I have a curious question regarding using bitfields and printing the
> values contained in them via the printf() function. Say we have the
> following definitions:

[Describes problem with the following]

>              long  bit1 : 10;   /* range = -512 to +511 */
>     unsigned long  bit2 : 10;   /* range =    0 to 1023 */

Chances are, you compiler is treating 'bit1' as unsigned.  Try
explicitly declaring it to be 'signed long'.  

For greater portability (and to be ANSI-legal), stick to 'signed int'
and 'unsigned int' for your bit-fields, and don't rely on whether
the high-order bit of a "plain" int bit-field will be treated
as a sign bit.

Walter
------



More information about the Comp.lang.c mailing list