Splitting up a bit into 2 x 4 bits

Posted on 2009-04-30
Last Modified: 2012-08-14
Hi All,

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

char * code = new char[1];, 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
    LVL 86

    Expert Comment

    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

    LVL 1

    Author Comment

    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

    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

    LVL 1

    Author Closing Comment

    Bang. There we go - awesome, thanks.
    LVL 53

    Expert Comment

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now