Ard Byte High
DodaTech
1 min read
In this tutorial, you'll learn about Arduino High Byte Extraction Returns Wrong Value. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Extracting the high byte from a 16-bit value gives incorrect results.
Quick Fix
Wrong
int val = 0xABCD;
byte h = val >> 8; // Works, but only for 16-bit
h = 0xAB (correct), but may fail for signed negative values.
Right
uint16_t val = 0xABCD;
byte highByte = highByte(val); // Returns 0xAB
byte lowByte = lowByte(val); // Returns 0xCD
Serial.print("High: 0x");
Serial.println(highByte, HEX);
Serial.print("Low: 0x");
Serial.println(lowByte, HEX);
High: 0xAB
Low: 0xCD
Prevention
Use the built-in highByte() and lowByte() macros for safe byte extraction. These work with uint16_t. For signed int values, cast to uint16_t first to avoid sign extension issues. For 32-bit values, use manual shifting: byte b3 = (val >> 24) & 0xFF.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro