Search code examples
c++byteuint8t

Need help understanding lowByte and highByte


I'm a beginner with C++ and I'd like to decipher some code:

    #define lowByte(w) ((uint8_t) ((w) & 0xff))
    #define highByte(w) ((uint8_t) ((w) >> 8))

It looks like variable declaration but I've never seen this syntax before. Can someone break these two lines down and tell me what they mean?


Solution

  • Those are macros declarations.

    Whenever you have lowByte(0x1234) in your code, it will be replaced with the right part of the macro, substituting arguments with their values, that is ((uint8_t) ((0x1234) & 0xff)).

    This step is performed by the preprocessor prior to compilation.