Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!agate!shelby!unix!hplabs!hp-ses!hpcuhb!hpcllla!hpclisp!hpclwjm!walter From: walter@hpclwjm.HP.COM (Walter Murray) Newsgroups: comp.lang.c Subject: Re: Bitfields and printf() Message-ID: <660076@hpclwjm.HP.COM> Date: 16 Feb 90 20:27:03 GMT References: <9002141141.AA04972@sorinc.UUCP> Organization: Hewlett-Packard Calif. Language Lab Lines: 21 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 ------