Learn / Digital logic / Logic gates

Binary, Hex and Bits in Practice

ReviewedBeginnerFreeAbout 6 min
ABY
In short

Binary, Hex and Bits in Practice: the short answer

Digital circuits only know two states, so they count in binary (base 2). Each position is worth twice the one to its right: 1, 2, 4, 8, 16, 32, 64, 128. Example: Data is ready, the reading is over range and the heater is on. Each bit is one yes/no flag.

Level
Beginner
Time
About 6 min
Topic
Digital logic
You will learn to

Count in binary and hexadecimal, convert between them, and set, clear and test single bits the way firmware does.

Digital circuits only know two states, so they count in binary (base 2). Each position is worth twice the one to its right: 1, 2, 4, 8, 16, 32, 64, 128. The binary number 1011 is 8 + 0 + 2 + 1 = 11. Eight bits make a byte, which holds 0 to 255. A microcontroller register is just a row of bits, and each bit often controls one thing, such as one pin.

Long binary numbers are hard to read, so programmers group them in fours and write each group as one hexadecimal (base 16) digit: 0 to 9, then A (10) to F (15). The byte 1010 0101 is A5 in hex, written 0xA5 in C. I2C addresses (0x3C for an OLED, 0x76 for a BME280), colours (0xFF8800) and register maps in datasheets all use hex, so reading it quickly saves a lot of time.

Firmware changes single bits with bitwise operators. To set bit n, OR with a mask: value |= (1 << n). To clear it, AND with the inverted mask: value &= ~(1 << n). To toggle it, XOR: value ^= (1 << n). To test it: if (value & (1 << n)). These are the same AND, OR, NOT and XOR gates you meet in hardware, applied to eight, sixteen or thirty-two bits at once.

Place value, with two symbols

Binary works like decimal but each place is worth double the one before: 1, 2, 4, 8, 16, and so on. So 1011 is 8 + 2 + 1 = 11. Eight bits make a byte and can hold 0 to 255, and 16 bits hold 0 to 65,535. Counting how many values fit is why a 10-bit ADC gives 1024 steps.

Hexadecimal is a shorthand, with one hex digit standing for exactly four bits. The digits go 0 to 9 and then A to F, so 0xFF is 1111 1111, which is 255, and 0x3C is 0011 1100.

Bit operations you will use

AND with a mask keeps chosen bits, OR sets bits, XOR flips them, and shifting left by one doubles a value. To test whether bit 3 is set, write value & (1 << 3). To set it, use value |= (1 << 3), and to clear it use value &= ~(1 << 3). Registers on a microcontroller and the data from sensors are packed this way.

A sensor that sends a 16-bit value as two bytes needs combining: (high << 8) | low. Byte order matters, so check the datasheet for which byte comes first.

Signed numbers and overflow

Negative numbers use two's complement, in which the top bit means negative. An 8-bit value of 0xFF is 255 if unsigned and −1 if signed. A counter has a limit too: an unsigned byte at 255 wraps to 0 when you add one, which is a classic source of bugs.

Common mistakes
  • Mixing up the byte order when combining two bytes from a sensor.
  • Using = instead of |= and clearing the other bits.
  • Forgetting that a variable wraps around at its maximum.
Key formulas
value = Σ bit × 2ⁿ
binary place values 1, 2, 4, 8, 16 …
1 hex digit = 4 bits
0xFF = 1111 1111 = 255
Worked example

Decode a sensor status byte of 0x2C

  1. 0x2C in binary: 2 = 0010, C = 1100, so the byte is 0010 1100.
  2. Counting from bit 0 on the right, bits 2, 3 and 5 are set.
  3. The datasheet says bit 2 = data ready, bit 3 = over-range, bit 5 = heater on.
  4. In code: if (status & (1 << 3)) { /* over-range */ }.

Result. Data is ready, the reading is over range and the heater is on. Each bit is one yes/no flag.

Try it

Write the numbers 0 to 15 in binary and hex in a table. Then, in an Arduino sketch, start with byte flags = 0; set bits 0 and 3, print it with Serial.println(flags, BIN) and Serial.println(flags, HEX), clear bit 0 and print again.

Check yourself

3 quick questions

  1. What is binary 1101 in decimal?

  2. What is 0xFF in decimal?

  3. Which line clears bit 4 of a variable x?

Common questions

What will I learn in "Binary, Hex and Bits in Practice"?

Count in binary and hexadecimal, convert between them, and set, clear and test single bits the way firmware does.

How do I decode a sensor status byte of 0x2C?

0x2C in binary: 2 = 0010, C = 1100, so the byte is 0010 1100. Counting from bit 0 on the right, bits 2, 3 and 5 are set. The datasheet says bit 2 = data ready, bit 3 = over-range, bit 5 = heater on. In code: if (status & (1 << 3)) { /* over-range */ }. Data is ready, the reading is over range and the heater is on. Each bit is one yes/no flag.

What is binary 1101 in decimal?

13. 8 + 4 + 0 + 1 = 13.

What is 0xFF in decimal?

255. F is 15, so 15 × 16 + 15 = 255, the largest byte.

Which line clears bit 4 of a variable x?

x &= ~(1 << 4);. AND with the inverted mask clears just that bit.