jad@insyte.uucp (Jill Diewald) (12/15/87)
Hi- We are porting our product from the HP running HPUX c, to the VAX running VMS c. The following simple program produces different answers on the two machines. We want to know which is right (if either) so we can report it as a bug to the correct source. Preferably, we want to know which the new ANSI standard thinks is correct. Thanks Jill Diewald Innovative Systems Techniques Newton, Ma .../harvard/axiom/insyte/jad /*--------------------------------------------------------------------- * The VAX distinguishes between signed and unsigned bit fields, the * HP does not. The following program will display '1' on the HP and * '-1' on the VAX. (To insure portability, all bit fields should be * declared as 'unsigned int'!) *--------------------------------------------------------------------- */ #include <stdio.h> main () { struct { int x : 1; } foo; foo.x = 1; printf ("%d\n", foo.x); }
chris@mimsy.UUCP (Chris Torek) (12/16/87)
In article <122@insyte.uucp> jad@insyte.uucp (Jill Diewald) writes:
-... HPUX c ... [and] VMS c ... [give] different answers. We want to
-know which is right (if either) so we can report it as a bug to the
-correct source.
...
-main() {
- struct { int x : 1; } foo;
-
- foo.x = 1;
- printf ("%d\n", foo.x);
-}
Whether bitfields are signed is undefined. I believe the current
draft says that to get a particular behaviour, you must use either
of the `signed' or `unsigned' keywords. In other words, the code
is wrong, not either of the compilers.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris
hitz@mips.UUCP (David Hitz) (12/19/87)
In article <9803@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: ] In article <122@insyte.uucp> jad@insyte.uucp (Jill Diewald) writes: ] ] ... HPUX c ... [and] VMS c ... [give] different answers. We want to ] ] know which is right (if either) so we can report it as a bug to the ] ] correct source. ] ... ] ] main() { ] ] struct { int x : 1; } foo; ] ] ] ] foo.x = 1; ] ] printf ("%d\n", foo.x); ] ] } ] ] Whether bitfields are signed is undefined. I believe the current ] draft says that to get a particular behaviour, you must use either ] of the `signed' or `unsigned' keywords. In other words, the code ] is wrong, not either of the compilers. Now I'm curious. Does ANSII require a hardware implementation to use 2s complement arithmetic for its integer representation? (Am I allowed to build a grey code machine?) If the standard doesn't say, then setting a single bit in a bitfield could result in an arbitrary integer. In K&R, the sections on bit fields and bit manipulation operators don't specify a particular representation. However, some examples do assume the following defines each map inidividual bits: #define FIELD1 1 #define FIELD2 2 #define FIELD3 4 which isn't true in an arbitrarary representation. And in the section on unsigned chars they do talk about sign extension which seems to imply 2s complement arithmetic. Some other C constructs are affected as well. For example, does the standard guarantee that ( (X << 1) == (X * 2))? Clearly there is too much code that relies on these features for them to change. I was just wondering if the standard is explicit. hitz -- Dave Hitz UUCP: {decvax,ucbvax,ihnp4}!decwrl!mips!hitz DDD: hitz@408-991-0345