dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Break down word into 4 bytes using shift and mask

  1. #1
    Join Date
    May 2009
    Posts
    41

    Break down word into 4 bytes using shift and mask

    If I have a word: unsigned int x and I'd like to break it down into four bytes: unsigned char b1, b2, b3, b4, can someone tell me the proper syntax on how to do this. I know you have to shift and then mask, but am not sure how to go about doing it from a code perspective.

    Code:
    b1 = x //&& mask all but the lowest order byte
    b2 = x >> 8 //&& mask all but the lowest order byte
    b3 = x >> 16 //&& mask all but the lowest order byte
    b4 = x >> 24//&& mask all but the lowest order byte
    ??

    Thanks.
    Last edited by CoDEFRo; March 7th, 2011 at 11:02 AM.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Break down word into 4 bytes using shift and mask

    You are asking a question and posting the solution at the same time ?

  3. #3
    Join Date
    May 2009
    Posts
    41

    Re: Break down word into 4 bytes using shift and mask

    Quote Originally Posted by Skizmo View Post
    You are asking a question and posting the solution at the same time ?
    I'm not sure what the correct syntax is for the mask I need and just wanted to make sure I did the shift correctly.

  4. #4
    Join Date
    Nov 2007
    Posts
    613

    Re: Break down word into 4 bytes using shift and mask

    Your code looks ok if you add masking.

    A WORD has only two bytes, you cannot break it into four. You need a DWORD to have four bytes.

    BTW, take a look at the following macros:
    LOWORD, HIWORD, LOBYTE, HIBYTE

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,651

    Re: Break down word into 4 bytes using shift and mask

    You don't need any masks but unless you use a very low compiler warning level you will get some warnings so you probably want to add a cast.
    Code:
    unsigned char b1 = static_cast<unsigned char>( x );
    unsigned char b2 = static_cast<unsigned char>( x >> 8 );
    unsigned char b3 = static_cast<unsigned char>( x >> 16 );
    unsigned char b4 = static_cast<unsigned char>( x >> 24 );
    Last edited by S_M_A; March 7th, 2011 at 06:12 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  6. #6
    Join Date
    May 2009
    Posts
    41

    Re: Break down word into 4 bytes using shift and mask

    ty

  7. #7
    Join Date
    May 2009
    Posts
    41

    Re: Break down word into 4 bytes using shift and mask

    Quote Originally Posted by srelu View Post
    Your code looks ok if you add masking.

    A WORD has only two bytes, you cannot break it into four. You need a DWORD to have four bytes.

    BTW, take a look at the following macros:
    LOWORD, HIWORD, LOBYTE, HIBYTE
    Thanks. Yeah DWORD is what I meant, 32 bits.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)