Slimelite BBS

Bit Fields - Save Memory

Back to R25C

From Matthew Probert · 1996-04-04 19:16 · #75202 in R25C

                              BIT FIELDS


                            Matthew Probert

                           Servile  Software


C allows the inclusion of variables with a size of less than eight 
bits to be included in structures. These variables are known as bit 
fields, and may be any declared size from one bit upwards. 

The general form for declaring a bit field is;

    type name : number_of_bits;


For example, to declare a set of status flags, each occupying one bit; 

typedef struct
{
    unsigned carry  : 1;
    unsigned zero   : 1;
    unsigned over   : 1;
    unsigned parity : 1;
} 
df;

df flags;

The variable 'flags' then occupies only four bits in memory, (although 
a complete byte will be assigned for the structure) and yet is 
comprised of four variables that may be accessed like any other 
structure field. 

A considerable saving in memory over the conventional use of 'int' 
types for flags which would use eight bytes of memory.



... [Servile Info Journal]
___ Blue Wave/QWK v2.12

--- WILDMAIL!/WC v4.12 
 * Origin: Ooh! BBS, London [MULTILINES]  0181-395-3108  (2:254/233.0)