magik@sorinc.UUCP (Darrin A. Hyrup) (02/14/90)
Greetings net.land, 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: long bit1 : 10; /* range = -512 to +511 */ unsigned long bit2 : 10; /* range = 0 to 1023 */ Then say if I do the following: bit1 = -1; bit2 = -1; The results should be: bit1 is -1; (since its signed) bit2 is 1023; (since its unsigned) The problem is that when I try to print the values of these variables: printf("Bit1 = %d, Bit2 = %d\n"); I get: "Bit1 = 1023, Bit2 = 1023" Does anyone know a switch to use in printf() or any other way I can use this function to print out the values? I can't even think of a way to cast a value into a bitfield (or emulate a bit width), in case the assignment of -1 to the bitfield variable is not working correctly either. (e.g., Trying to assign value 0xFFFF to a 10 bit variable isn't going to fit right. I don't think it works as I assigned it to -1 (via requesting the value of -1 from the keyboard) and compared it to -1 it came back false. Probably due to the bitwidth differences again). This came up in a project I have in the background right now. I am trying to avoid having to write my own printing routine for this. And if the assignment doesnt work either, that could cause some problems. The compiler I'm using is not very smart sometimes, and many things don't work unless cast correctly (does do SOME autocasting, but not much). Any ideas would be appreciated. Thanks, Darrin Hyrup -- Darrin A. Hyrup // AMIGA Enthusiast rencon!esfenn!dah magik@sorinc.PacBell.COM \X/ & Software Developer pacbell!sorinc!magik ========================================================================== "Speak little and well, if you wish to be considered as possessing merit."
walter@hpclwjm.HP.COM (Walter Murray) (02/17/90)
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 ------
magik@sorinc.UUCP (Darrin A. Hyrup) (02/20/90)
In article <660076@hpclwjm.HP.COM> walter@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@sorinc.PacBell.COM \X/ & Software Developer pacbell!sorinc!magik ========================================================================== "Speak little and well, if you wish to be considered as possessing merit."