Binary & Number Systems

Binary, hexadecimal and octal; converting between bases; two's complement for negative numbers; and overflow.

Beginner 4 min readLesson 2 of 8 Interview Q&A

Computers use binary (base 2) because a transistor has two reliable states, off and on. Everything else — integers, text, images, programs — is built on top of patterns of 0s and 1s. You don't have to compute in binary all day, but you need to be comfortable reading it.

Positional number systems

In any base b, each digit is worth the base raised to its position:

10112=1⋅23+0⋅22+1⋅21+1⋅20=8+0+2+1=11101011_2 = 1\cdot2^3 + 0\cdot2^2 + 1\cdot2^1 + 1\cdot2^0 = 8 + 0 + 2 + 1 = 11_{10}
07060504130211100B (11)
11 in one byte = 0x0B
BaseNameDigitsPrefix in codeExample (decimal 255)
2Binary0–10b0b11111111
8Octal0–70o / leading 00o377
10Decimal0–9—255
16Hexadecimal0–9, A–F0x0xFF

Why hexadecimal?

One hex digit is exactly 4 bits (a nibble), so two hex digits are one byte. Hex is a compact, human-friendly way to write binary: memory addresses (0x7ffe…), colours (#FF8800), hashes and byte dumps all use it.

HexBinaryHexBinary
0000081000
1000191001
20010A1010
30011B1011
40100C1100
50101D1101
60110E1110
70111F1111

To convert binary ↔ hex, group bits in fours from the right: 1101 0110 = D6.

Converting decimal to binary

Repeatedly divide by 2 and read the remainders from bottom to top.

  1. 13 ÷ 2 = 6 remainder 1
  2. 6 ÷ 2 = 3 remainder 0
  3. 3 ÷ 2 = 1 remainder 1
  4. 1 ÷ 2 = 0 remainder 1 → read upwards: 1101
Integer.toBinaryString(13);      // "1101"
Integer.toHexString(255);        // "ff"
Integer.parseInt("1101", 2);     // 13
Integer.parseInt("ff", 16);      // 255

Bits, bytes and sizes

  • bit — one 0 or 1.
  • byte — 8 bits, 256 possible values (0–255). The smallest addressable unit of memory.
  • word — the CPU's natural size, usually 64 bits today.
n bitsDistinct valuesUnsigned range
82560 – 255
1665,5360 – 65,535
32~4.29 billion0 – 4,294,967,295
64~1.8 × 10¹⁹0 – 18,446,744,073,709,551,615

Negative numbers: two's complement

Almost every CPU stores signed integers in two's complement. With n bits, the top bit has a negative weight: −2ⁿ⁻¹.

1716151413021110−128+123
−128 + 123 = −5

To negate a number: invert all the bits and add 1.

  1. 5 = 00000101
  2. Invert: 11111010
  3. Add 1: 11111011 = −5

Why this design won:

  • There's only one zero (unlike sign-magnitude, which has +0 and −0).
  • Addition works unchanged for signed and unsigned values: the same adder circuit handles both.
  • The range of n bits is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1. For 8 bits that's −128 to 127; for 32 bits, about ±2.1 billion.

Overflow

Fixed-size integers wrap around when they exceed their range.

int x = Integer.MAX_VALUE;   // 2147483647
x + 1;                       // -2147483648  (silent wrap)
Math.addExact(x, 1);         // throws ArithmeticException
long safe = (long) x + 1;    // 2147483648
Key takeaways
  • Binary is base 2; each position is a power of two.
  • Hex digit = 4 bits; two hex digits = one byte.
  • Convert decimal → binary by repeated division by 2.
  • Two's complement: invert and add 1 to negate; the top bit is −2ⁿ⁻¹.
  • Fixed-width integers overflow and wrap; know your language's behaviour.
Interview questions
QHow do you check whether a number is a power of two?easy

n > 0 && (n & (n - 1)) == 0. A power of two has exactly one set bit, and subtracting 1 flips that bit and all the bits below it.

QWhy is the range of a signed byte −128 to 127 rather than symmetric?medium

There are 256 patterns. Zero takes one of the non-negative patterns, leaving 127 positive values and 128 negative ones. In two's complement −128 (10000000) has no positive counterpart, so negating it overflows back to −128.