www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/15/04:15:24

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Bit fields in djgpp
Date: Thu, 15 Jan 1998 00:31:55 -0800
Organization: Alcyone Systems
Lines: 37
Message-ID: <34BDC97B.5453B8F8@alcyone.com>
References: <Chameleon DOT 980113164922 DOT nrotem AT netvision DOT netvision>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019