Question on Data Byte and Word Manipulation



  • Avatar
    Michael Freeman

    Use shift operator >>


    ByteWanted = (unsigned char)((LongData >> 0) & 0xff); //Always gets least significant byte


    ByteWanted = (unsigned char)((LongData >> 24) & 0xff); //Always gets most significant byte 


    This works no mater what endian the CPU is.




    Here are the macros I use:

    #define word_get_low_byte(val) ((unsigned char)((val) & 0xFF))


    #define word_get_high_byte(val) ((unsigned char)(((val)>>8) & 0xFF))


    #define dword_get_low_byte(val) ((unsigned char)((val) & 0xFF))


    #define dword_get_mlb_byte(val) ((unsigned char)(((val)>>8) & 0xFF))


    #define dword_get_mhb_byte(val) ((unsigned char)(((val)>>16) & 0xFF))


    #define dword_get_high_byte(val) ((unsigned char)(((val)>>24) & 0xFF))





    Comment actions Permalink
  • Avatar
    Graham Cole

    Shift and AND is the way to go. It is fully portable and, in the case of ARM, it is efficiently handled - the ARM can shift by any number of bits in a single instruction.

    Comment actions Permalink
  • Avatar
    Steve Tulk

    Thank You both for the replies. I was not aware that ARM did single cycle shifts of any size. That being the case, it is no doubt the way to go.



    Comment actions Permalink

Please sign in to leave a comment.