Bit Masking Question
Hopelessly in love w/Donna Reed
angst at cs.ucsb.edu
Sun Jun 23 17:50:18 AEST 1991
This is not necessarily a C question, but since I'm programming it C,
I can ask in that context.
What I'm doing is reading characters from a file and I'd like to
categorise the characters into 3 groups based on their 2 most-
significant bits. Group 1 has '11' in their MSb's, group 2 has
'10' in their MSb's, and group 3 has '0X' (X = don't care) in
their MSb's.
Here's how I'm currently doing it:
#define FMASK 0xC0
#define RMASK 0x80
int c;
...
switch (c & FMASK) {
case FMASK:
Do stuff for group 1
...
case REPMASK:
Do stuff for group 2
...
case 0x0:
case 0x40:
Do stuff for group 3
...
default:
fprintf (stderr, "bad news!\n");
}
This seems to work, but I'd like comments. Is there a better way to
do this? Should I make #define's for 0x0 and 0x40? Is there a way
to #define bits?o
Thanks in advance for any help!
"Let the fools have their tartar sauce." | Dave Stein
- Mr. Burns | angst at cs.ucsb.edu
| angst%cs at ucsbuxa.bitnet
More information about the Comp.lang.c
mailing list