Thread: Getting high-low bytes

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Getting high-low bytes

    HI, I'm currently working on a project where I need to split an unsigned 16bit int into bytes the code I have written is:

    Code:
    msg[AXIS_HI]=value>>8;
    msg[AXIS_LO]=(uint8)value;
    The software I'm working on provides various macros for byte manipulation there is one macro that splits 16 bit ints;

    Code:
    #define HI_UINT16(a) (((a) >> 8) & 0xFF)
    #define LO_UINT16(a) ((a) & 0xFF)
    I'm a C novice so I'm not sure what the purpose of the and operation is I understand what it does I'm just not sure why it's necessary.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    For LO_UINT16(), “and”ing with 0xff gives you the bottom byte. It's necessary, I think, for obvious reasons: if you didn't do it, you'd get the whole value you passed in. If you're wondering why to mask instead of cast, I would suggest that the mask gets the intent across, while the cast does not. A cast could be there for a number of reasons, while a mask with 0xff is very clear.

    If you always pass an unsigned, 16-bit type to HI_UINT16() (or at the very least a value in that range), the masking with 0xff is not necessary. There are two reasons I can think of why one might use the mask here, though:
    1. If a larger value gets passed in, simply right-shifting by 8 won't result in an 8-bit value (0x100000 >> 8 is 0x1000, for example). However, it'd be wrong to pass in a larger value, so there's a bug either way.
    2. Semantics. You clearly want the bottom 8 bits after shifting, so the mask makes that explicit. We know that the top 8 bits will be empty, but it might be clearer to a reader to see the mask with 0xff.

    I would definitely use a mask over a cast for getting the bottom 8 bits. I don't think one method (masking vs. not) is clearly superior for getting the top 8 bits. Depends on how explicit you want to be.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks for that cas.

    Coding in C continues to be an interesting experience....

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course threre's always the "union" method...
    Code:
    union twobytes
       {  unint_16  Word;
           struct  bytes
              { uint_8  HiByte;
                 uint_8  LoByte; } }
    stuff in a word ... s.Word = myval; ... read the bytes ... mybyte = s.bytes.LoByte;

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    So the word is accessible via the struct members because its a union?
    I'm familiar with structs but not so much with unions.
    Last edited by venkman; 10-10-2010 at 08:23 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by venkman View Post
    So the word is accessible via the struct members because its a union?
    I'm familiar with structs but not so much with unions.
    The idea of a union is that two or more variables occupy the same memory address. Since they occupy the same space, they become interchangeable... Two bytes or 1 word same thing.

    The union simply describes a Word that occupies the same memory address as 2 Bytes... Therefore you can put in a word and get two bytes out or you can reverse it and put in two bytes and get a word out...

    Of course there are other uses for unions, but I've always liked the cleverness of this one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:01 AM