Bitfields and printf()

Darrin A. Hyrup magik at sorinc.UUCP
Tue Feb 20 19:17:49 AEST 1990


In article <660076 at hpclwjm.HP.COM> walter at hpclwjm.HP.COM (Walter Murray) writes:
>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.

Thanks for all the letters folks. The problem seems to be in the compiler
itself, rather than in what I was doing with it. The documentation clearly
states that the compiler does support unsigned and signed bitfields, and
that unsigned bitfields may be specified with or without the "signed"
keyword. After testing all this out, that has been proven to be incorrect.
(Chances are that the manual may have been written with future features in
mind or something).

I was hoping that it was a problem is just not using the correct conversion
switch in the printf() routine, but after the series of tests I made, it is
certain that the compiler is not allowing signed bitfields as documented.
However, now that I know this I can take the proper steps. (And no, this is
not a PC compiler, its the mainframe at work.)

Again, thanks for all your help.

        Darrin Hyrup
--
Darrin A. Hyrup              // AMIGA Enthusiast         rencon!esfenn!dah
magik at sorinc.PacBell.COM   \X/ & Software Developer   pacbell!sorinc!magik
==========================================================================
"Speak little and well, if you wish to be considered as possessing merit."



More information about the Comp.lang.c mailing list