| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Bit-sized variables |
Hi Anton - in this echo, just ask "All", there's heaps of us out
here...
AM> Now, what I need to know is how you break a variable down into
AM> individual bits, or small groups of bits that can be individually
AM> accessed as though they were part of a structure - the original
AM> variable. I read somewhere that this can be done so that each part
AM> doesn't necessarily have to belong to a specific named variable - it
AM> may have been done with UNIONS (which I don't know much about).
C doesn't seem to support binary variables directly, like the way
it handles decimal or hex values. But here's a small example from
something I'm working on at the moment:
My program has just received an 8 bit byte over a serial link from
a microprocessor sprinkler controller. This byte contains the
active days for a particular solenoid in the lower 7 bits of this
byte, eg if every day was active, the byte would be 01111111
binary, or if only Monday and Tuesday were active, it would be
00110000b. My program wants to turn the byte into a human readable
string, like "SMTWTFS" for the first byte, and " MT " for the
second one.
(In the following, I name the 8 bits in a byte 0 to 7. The least
significant bit (right hand end) is bit 0, the most significant bit
(left end) is bit 7.)
------
unsigned char k; /* could be char or int, uchar is tidier... */
int j;
char days[] = "SMTWTFS"; /* string I'll pinch day initials from */
char outstring[8]; /* destination string */
k = events[i].pump_days; /* the byte with the days in it from
the comms section of the program.
events[] is an array of structures,
irrelevant here. */
for(j=0;j<7;j++) /* loop through 7 times */
{
k = k << 1; /* shift k left one bit, so bit 6 will be
in bit 7's place */
if(k & 0x80) /* logical AND k with 10000000 binary,
which = 80 hex. Result will be true
if bit 7 in k is a 1. */
{ outstring[j] = days[j]} /* do this on true result */
else
{ outstring[j] = " "} /* do this on false result */
}
outstring[7] = '\0'; /* gotta terminate the string */
------
The gory longhand version, without the left shift loop,
runs something like
------
if(k & 0x40)
outstring[0]="S";
else
outstring[0]=" ";
if(k & 0x20)
outstring[0]="M";
else
outstring[0]=" ";
if(k & 0x10)
outstring[0]="T";
else
outstring[0]=" ";
if(k & 0x08)
outstring[0]="W";
else
outstring[0]=" ";
if(k & 0x04)
outstring[0]="T";
else
outstring[0]=" ";
if(k & 0x02)
outstring[0]="F";
else
outstring[0]=" ";
if(k & 0x01)
outstring[0]="S";
else
outstring[0]=" ";
outstring[7] = " ";
------
(note I didn't start with if(k & 0x80) here, cos I haven't left
shifted k at all.)
The way I see it, "one bit" variables aren't needed here, cos it's
quite easy to detect and/or change the state of any bit in a char
or integer variable.
Cheers
--- PPoint 1.88
* Origin: Silicon Heaven (3:711/934.16)SEEN-BY: 711/809 934 @PATH: 711/934 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
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™.