This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Accessing individual bytes of an integer.

This should be an easy question, but it's driving me nuts.

I'm programming in C51. I need to access the high-byte and the low-byte of a unsigned short variable. How should I do it?

  • "How should I do it?" (my emphasis)

    Well, that depends on your criteria for "good" coding practice!

    There is a Portable way, and a non-portable (but probably quicker) way:

    The portable way is to mask-off the required byte (High or Low), and to shift the high byte down into the low byte;

    The non-portable way is to use a union containing the integer and the two separate bytes.
    This is non-portable because it relies upon the internal data representation used by C51 - for which you will have to consult the manual.
    The same result can be obtained using an Abstract Pointer (qv).

    Note that these techniques are not specific to C51; they are standard ANSI 'C' - the only C51-specific bit is knowing the data representation for the non-portable approach.