TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: RICK BISCHOFF
from: JEAN-POL LANDRAIN
date: 1997-04-03 08:45:00
subject: Bit Fields

Salut Rick,
Dans un msg du , Jani Saksa ‚crit … Rick Bischoff :
 RB>> Does anyone know how to declare a variable that holds only one
 RB>> bit of information.. (You know only two values (0 or 1)) ???
 JS> That's easy but it will always take one byte of memory. just use
 JS> normal char variable. When it's 0 then the bit is off and when it's 1
 JS> then bit is on (0=00000000 1=00000001 2=00000010 3=00000011...).
Better ... use a struct like this :
for 8 bits :
------------
struct bit_field
    {
    unsigned char bit1 : 1;
    unsigned char bit2 : 1;
    unsigned char bit3_6 : 4;
    unsigned char bit7_8 : 2;
    };
union byte
    {
    unsigned char All;
    struct bit_field Bit;
    };
union byte var;
So, you can use each bit like you want :
var.All=x; // x is 0 (00000000b) to 255 (11111111b)
var.Bit.bit1=x; // x is 0 (0b) or 1 (1b)
var.Bit.bit7_8=x; // x is 0 (00b) to 3 (11b)
If you want a 16 bits struct, you just have to replace unsigned char by 
unsigned short.
I hope It's what you were looking for ...
Amiti‚s,
Jean-Pol
--- GoldED/386 2.50+
---------------
* Origin: Mininet 97 +32(0)87/88-33-44 - Cimes (2:293/4004.104)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.