Mail Archives: djgpp/1998/01/15/04:15:24
Noam Rotem wrote:
> struct
> {
> int x : 1;
> int y : 1;
> int z : 1;
> }Test={0,0,0};
>
> main()
> {
> ....
> Test.x=1;
> printf("%d",Test.x);
> ...
> }
>
> I usually get -1 as an output, or other non zero values.
>
> I can live with non zero undefined values, but I would like
> to know the reason for the results I get.
The results you get a correct. You have declared the bitfields in
question as signed, and for a 1 bit signed field, the only possible
values are 0 and -1. Assigning 1 to a 1 bit signed field results in
automatic conversion, and so Test.x gets the value -1.
If you wanted them to really be "bits" (0 or 1) in the traditional
sense, you should have declared the bitfields unsigned.
--
Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
Alcyone Systems / http://www.alcyone.com/max/
San Jose, California, United States / icbm://+37.20.07/-121.53.38
\
"Life may be / the product of imperfections."
/ (Marclo Gleiser)
- Raw text -