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

x
Solved

# Splitting up a bit into 2 x 4 bits

Posted on 2009-04-30
Medium Priority
368 Views
Hi All,

I'm reading in some data from a file like so:

char * code = new char[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
0
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);
``````
0

LVL 1

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?
0

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);
}
``````
0

LVL 1

Author Closing Comment

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

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 !!
0

## Featured Post

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++.
###### Suggested Courses
Course of the Month18 days, 6 hours left to enroll

#### 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.