5-min deep dive

Binary in depth: hex, negative numbers and units

Once you understand binary, next come hexadecimal and negative numbers. Both are exam staples, and you see them almost every day on the job.

Reading time: about 5 min

← Start with the 1-min version

Hex turns 4 binary digits into 1 character

Binary numbers get long. So you split them into groups of 4 digits (4 bits) and write each 0–15 as one character: that’s hexadecimal. 10–15 are written as A–F.

BinaryHexDecimal
100199
1010A10
1101D13
1111F15

For example, 1010 0101 read in groups of four is A and 5, so “A5.” In decimal that’s 10×16 + 5 = 165. 8 bits (1 byte) is exactly 2 hex digits, so a byte can be written as 00–FF (0–255).

Hex is everywhere

Negative numbers use “two’s complement”

Computers want to do subtraction with the same circuit as addition. So with 8 bits, “the number that adds up to exactly 1 0000 0000 (256)” is used as the negative number. This is called two’s complement. You make it in two steps:

  1. Flip every bit (swap 0s and 1s)
  2. Add 1
  5 = 0000 0101
flip   → 1111 1010
+1     → 1111 1011   ← this is −5

check:  0000 0101
      + 1111 1011
     =1 0000 0000
drop the overflowing 1 → 0000 0000 = 0

If the leftmost bit is 1, the number is negative. The range 8 bits can hold becomes −128 to 127.

Shift the digits to double or halve

Shifting a binary number one place left (left shift) doubles it; shifting right (right shift) halves it (dropping any remainder). It’s the same as adding a 0 on the right of a decimal number to multiply by 10. Shift 0000 0011 (3) left by 2 bits and you get 0000 1100 (12) = 4 times.

Why 1 TB looks like 931 GB

Because disk sellers usually count TB and GB in steps of 1000, while operating systems like Windows count in steps of 1024. Divide 1 TB = 1,000,000,000,000 bytes by 1024×1024×1024 and you get about 931. To tell them apart, the 1024-based units are written KiB, MiB, GiB (kibi, mebi, gibi).

Field note: why IPv4 addresses ran out

An IPv4 address is 32 bits, so there can be 2³² ≈ 4.3 billion of them — fewer than the world’s population, and with more smartphones and servers they ran out. So 128-bit IPv6 was created. 2¹²⁸ is about 340 undecillion (a number with about 39 digits) — practically impossible to use up.

Practice questions

Four-choice questions in the style of Japan’s Fundamental Information Technology Engineer Examination. Tap below to see the answer.

Q1Which is the hexadecimal “A5” written in decimal?

  • A105
  • B155
  • C165
  • D175
Show the answer and explanation

Answer: CA is 10, so 10×16 + 5 = 165. In binary it’s 1010 0101.

Q2Which represents −1 in 8-bit two’s complement?

  • A0000 0001
  • B1000 0001
  • C1111 1110
  • D1111 1111
Show the answer and explanation

Answer: DFlip all the bits of 1 (0000 0001) to get 1111 1110, then add 1 to get 1111 1111. Adding 0000 0001 to 1111 1111 gives exactly 0 in 8 bits.

Q3A positive integer written in binary is shifted left by 3 bits. How many times the original number is the result? Assume no overflow occurs.

  • A3 times
  • B6 times
  • C8 times
  • D9 times
Show the answer and explanation

Answer: CEach 1-bit left shift doubles the number, so 3 bits makes it 2×2×2 = 8 times.

Summary
  • Hex writes 4 binary digits as 1 character. 1 byte = 2 hex digits (00–FF)
  • Negatives use two’s complement: flip and add 1. 8 bits hold −128 to 127
  • Left shift doubles, right shift halves. Capacity is counted two ways: 1000 and 1024