Description

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.

Syntax

Use the following function to extract the most significant 8 bits from a 16 bit variable:

highByte(x)

Parameters

The function admits the following parameter:

x: input variable to extract from. Data type: any type.

Returns

The function returns the byte extracted from the original variable. Data type: byte.

Example Code

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() {
}

Note

This is what the highByte() function does behind the scenes:

#define highByte(w) ((uint8_t) (((w) >> 8) & 0xFF))

See also