AndreeaN
asked on
Splitting up a bit into 2 x 4 bits
Hi All,
I'm reading in some data from a file like so:
char * code = new char[1];
infile.read(code, 1);
Within this byte of data there are two numbers I need to extract - they are represented by the first 4 bits and the second four bits - how to go about splitting the byte up and extracting both numbers?
Thanks
I'm reading in some data from a file like so:
char * code = new char[1];
infile.read(code, 1);
Within this byte of data there are two numbers I need to extract - they are represented by the first 4 bits and the second four bits - how to go about splitting the byte up and extracting both numbers?
Thanks
ASKER
Hmm, I have no doubt that will work, but since I have not done any bitmasking before, could you explain how what you have there extracts the two parts? Also, if possible, could you show me a non-macroed solution?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bang. There we go - awesome, thanks.
If you use that solution (with a right-shift in it), then make sure to use an unsigned char, and not just a char. As a rule, when bit shifting is involved, always use unsigned integer types !!
Open in new window