www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/06/18/03:15:35

From: sparhawk AT eunet DOT at (Gerhard Gruber)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: enum problem
Date: Wed, 17 Jun 1998 23:20:29 GMT
Organization: Customer of EUnet Austria
Lines: 49
Message-ID: <358d472e.3524128@news.Austria.EU.net>
References: <bWLoegW7sFse-pn2-0JB8f0w8xHdR AT portX61 DOT lanzen DOT net> <358745c4 DOT 10851789 AT news DOT Austria DOT EU DOT net> <bWLoegW7sFse-pn2-QHVDOxxt8xtV AT portF19 DOT Generation DOT NET>
NNTP-Posting-Host: e056.dynamic.vienna.at.eu.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Destination: sl AT psycode DOT com DOT NOSPAM (Gili)
From: Gruber Gerhard
Group: comp.os.msdos.djgpp
Date: 17 Jun 1998 14:18:48 GMT:

>On Mon, 15 Jun 1998 18:31:52, sparhawk AT eunet DOT at (Gerhard Gruber) 
>wrote:
>
>> usually) or, if you want to use the enum only as bits then you could use
>> bitfields. This way you can identify each value and you don't have to bother
>> to or them together (or and them apart).
>
>	Tell me more about these bitfields and provide an example if 
>possible. Thank you,

typedef struct {
    UBYTE InUse : 1;    // Set when an irq is allocated
    UBYTE Opened : 1;    // port is opened
    UBYTE Transfer : 1;    // transfer in progress
    UBYTE IOMode : 1;    // TRUE = Reading, FALSE = writing
    UBYTE Error : 1;    // error indicator
} SerState;

You can define more bits if neccessary, I just happend to use 1 per Value. I
recommend to use unsigned char for bitfields because this way the total size
of the structure is just as big as necessary to hold all the bits (in this
case 1 byte). If you use unsigned int then the minimum size the structure can
be is the size of an int. I recomment to use unsigned types though, because a
single bit would represent 0 or -1 instead of 0 or 1 and this might be
confusing.

The usage is simple. Just like any other structure with "normal" types.

i.e.:
SerState st;

st.InUse = TRUE;
...
if(st.InUse == TRUE)
   do something;

--
Bye,
   Gerhard

email: sparhawk AT eunet DOT at
       g DOT gruber AT sis DOT co DOT at

Spelling corrections are appreciated.

- Raw text -


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