avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] Bit Variables


From: Heinrich Vermeulen
Subject: RE: [avr-gcc-list] Bit Variables
Date: Wed, 12 Jun 2002 09:06:25 +0200

You can use bitfields in a structure.  This will also keep all your bits
together, especially when using it as global vars.
E.g.
typedef struct {
 unsigned bit_x : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_y : 1;  //bit is called bit_x reserving 1 bit location  
 unsigned bit_z : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_a : 1;  //bit is called bit_x reserving 1 bit location
 unsigned bit_b : 2;  //bit is called bit_x reserving 2 bit locations
 unsigned bit_c : 2;  //bit is called bit_x reserving 2 bit locations
 unsigned bit_d : 4;  //bit is called bit_x reserving 4 bit locations
 unsigned bit_e : 4;  //bit is called bit_x reserving 4 bit locations
} BitField;

/*
2 bytes will be reserved for a var of this type (count the bits above)
The number of bits reserved will be:
bytes reserved = nbits/8 + (nbits%8 > 0)
IOW 1 byte for every 8 bits or part thereof
*/ 

main()
{
  BitField myBitField; 

  myBitField.bit_x = 1; //can be assigned a value of 0 to 1  
  myBitField.bit_b = 3; //can be assigned a value of 0 to 3
  myBitField.bit_e = 12; //can be assigned a value of 0 to 15
}

Heinrich Vermeulen


> -----Original Message-----
> From: Michael Bellefeuille [mailto:address@hidden
> Sent: 11 June 2002 07:42
> To: avr-gcc-list
> Subject: [avr-gcc-list] Bit Variables
> 
> 
> Hi,
>     What is the best way of handing bit variables?  I found the AVR 
> Freaks design note #008, but it is about handling i/o with bitfields.
> 
> Michael
> 
> 
> avr-gcc-list at http://avr1.org
> 
avr-gcc-list at http://avr1.org



reply via email to

[Prev in Thread] Current Thread [Next in Thread]