Extracts the high (most significant) 8 bits from a 16-bit integer (int, unsigned int, etc.), or the second lowest byte of a larger data type. Useful when you are dealing with communication protocols (like I2C, SPI, serial) where data must be sent in bytes, or when manually splitting numbers.
Use the following function to extract the most significant 8 bits from a 16 bit variable:
highByte(x)
The function admits the following parameter:
x: input variable to extract from. Data type: any type.
The function returns the byte extracted from the original variable. Data type: byte.
unsigned int x = 0xABCD; // Hexadecimal 43981
void setup() {
Serial.begin(9600);
byte high = highByte(x); // Extracts 0xAB (171 decimal)
Serial.print("The high byte is: ");
Serial.println(high, HEX); // Prints "AB"
}
void loop() {
}
This is what the highByte() function does behind the scenes:
#define highByte(w) ((uint8_t) (((w) >> 8) & 0xFF))