All articles

    Binary and Hexadecimal: the Numbers a Microcontroller Understands

    Number systems explained for beginners: the bit, byte and word, converting from binary and hex to decimal and back step by step, why we use 0x, negative numbers and overflow, examples from I2C addresses, colours and registers, bitwise operations in code, combining two bytes into one number, and printing to Serial in different bases.

    Updated 5 min read

    When you read a datasheet or code, you'll find numbers like 0x3C, 0b10110010 and 0xFF. There's nothing mysterious about them; they're the same numbers you know, just written in a way closer to what the microcontroller understands.

    First: decimal itself

    We count with 10 digits (0 to 9), and each place is worth ten times the one beside it: 345 = 3 × 100 + 4 × 10 + 5 × 1. Binary and hex are exactly the same idea, just with a different number of digits: binary with two, and hex with 16.

    Binary

    The microcontroller understands only two states: 0 and 1 (low and high voltage). So every number is written with places worth 1, 2, 4, 8, 16… (each place double the one before).

    8 bits worth 128, 64, 32… and 1. The number = the sum of the places that hold a 1
    8 bits worth 128, 64, 32… and 1. The number = the sum of the places that hold a 1

    An example: 0b00000101 = 4 + 1 = 5.

    • Bit: a single place.
    • Byte: 8 bits, from 0 to 255.

    From decimal to binary

    Divide by 2 and take the remainder, repeat, and read the remainders from bottom to top. Or the easier way: start from the largest place that fits into the number. For example 13 = 8 + 4 + 1 = 1101.

    A longer example: 200

    1. The largest place that fits: 128 → 72 left.
    2. 64 fits → 8 left.
    3. 32 and 16 don't; 8 fits → 0 left.
    4. The result: 128 + 64 + 8 = 11001000.

    Common sizes

    NameBitsRange (unsigned)On the Arduino
    Byte80 to 255byte or uint8_t
    Word160 to 65535unsigned int on the Uno
    Double word320 to about 4.29 billionunsigned long

    That's why the Uno's ADC (10-bit) gives 0 to 1023: because 2 to the power 10 = 1024. And the ESP32's (12-bit) gives 0 to 4095. See the ADC.

    Hexadecimal (hex)

    Binary is long and hard to read. So every 4 bits are written as one hex digit (from 0 to F):

    DecimalBinaryHex
    000000
    910019
    101010A
    151111F

    So a byte is always two hex digits:

    • 0xFF = 1111 1111 = 255
    • 0x3C = 0011 1100 = 3 × 16 + 12 = 60
    • 0x80 = 1000 0000 = 128

    The 0x at the start means "this number is hex", and 0b means "binary".

    From binary to hex in a second

    Split the bits into groups of 4 from the right, and convert each group on its own:

    11001000 → 1100 and 1000 → C and 8 → 0xC8 = 200.

    That's why programmers like hex: converting to binary happens in your head, without a calculator.

    Where will you meet them?

    • I2C addresses: an OLED display at 0x3C, and an RTC at 0x68. See I2C.
    • Colours: #FF8800 = red 255, green 136 and blue 0.
    • Chip registers in the datasheet: each bit in the register has a function.
    • The shift register: each bit controls an output.

    Negative numbers and overflow

    • The int8_t (or char) type uses the same 8 bits, but the range is −128 to 127. So the byte 0xFF can be read as 255 or −1 depending on the type.
    • Overflow: if byte x = 255; and you add 1, it becomes 0, not 256, because it "wraps". And the same happens with the Uno's int (16-bit): 32767 + 1 = −32768!

    This is the cause of many strange calculation errors. And it's why millis() returns an unsigned long, and wraps after about 49 days. See millis instead of delay.

    Bitwise operations in code

    byte x = 0b00000000;
    x |=  (1 << 3);   // set bit 3
    x &= ~(1 << 3);   // clear bit 3
    x ^=  (1 << 3);   // toggle bit 3
    bool on = x & (1 << 3);  // is bit 3 set?
    • <<: shift left (each shift = multiply by 2).
    • |, &, ^ and ~: OR, AND, XOR and NOT on all the bits, like logic gates.

    Two bytes = one number

    Many sensors send the reading in two bytes (high and low). You combine them like this:

    byte hi = Wire.read();
    byte lo = Wire.read();
    int value = (hi << 8) | lo;   // the high byte shifts 8 places, and the low byte fills in

    For example hi = 0x01 and lo = 0x2C → 0x012C = 300. And the reverse: highByte(v) and lowByte(v) split the number into two bytes.

    Print in any base

    Serial.println(200, BIN);   // 11001000
    Serial.println(200, HEX);   // C8
    Serial.println(200, DEC);   // 200

    Very useful when you're reading a chip's registers or debugging I2C. See the Serial Monitor.

    Advice

    The computer's calculator (in Programmer mode on Windows) converts between the three systems in a second. Use it until you get used to them.

    Memorise these and you'll be fine: 2 to the power 8 = 256, 2 to the power 10 = 1024, 0xFF = 255, 0x80 = 128, and 0x10 = 16.

    FAQ

    Why not just write hex all the time? When every bit has a meaning (such as a configuration register or shift register outputs), binary 0b makes clear which bit is set. And for a large number or an address, hex is shorter.

    What does 7-bit mean in an I2C address? The real I2C address is 7 bits (0 to 127), and the eighth bit is for read/write. So if you find 0x78 in a datasheet instead of 0x3C, it's the same address shifted one place: 0x3C << 1 = 0x78. The Wire library takes the 7-bit value.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.