[Last Call] Learn how to a build a cloud-first strategyRegister Now


Splitting up a bit into 2 x 4 bits

Posted on 2009-04-30
Medium Priority
Last Modified: 2012-08-14
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?

Question by:AndreeaN
  • 2
  • 2
LVL 86

Expert Comment

ID: 24274504
You can do that using the following macros, which in turn use bit masking and shifting:
#define LO(x) (x & 0x0F) // extracts the lower four bits
#define HI(x) ((x & 0xF0) >> 4) // extracts the upper four bits and shifts them down by 4
int upper = HI(*code);
int lower = LO(*code);

Open in new window


Author Comment

ID: 24274538
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?
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 24274598
Well, just consider the hex representations of the upper and lower four bits in a byte, which are 0xF0 (upper) and 0x0F (lower). Now, by using a bitwise AND, you'll ignore the respective other parts, e.g.

BYTE b = <something>;

BYTE lower = b & 0x0F; // mask out the lower 4 bits

BYTE upper = b & 0xF0; // mask out the upper 4 bits

The problem for the upper part is that the have an offseet 0f 32 dec, so the need to be 'bitshifted', i.e.

upper = upper >> 4;

A non-macro solution qcould be the following, yet macros are IMHO the weapon of choice in this case for performance rasons:
void SplitByte(BYTE b, BYTE* upper, BYTE* lower) {
  *lower = (x & 0x0F);
  *upper = ((x & 0xF0) >> 4);

Open in new window


Author Closing Comment

ID: 31576671
Bang. There we go - awesome, thanks.
LVL 53

Expert Comment

ID: 24277175
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 !!

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question