Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 55 ยท Data Representation

Binary, Octal and Hexadecimal

Learn positional number systems: convert between decimal, binary, octal and hex by hand and in code, understand two's complement for negatives, and see how overflow happens.

You count in base 10 because people have ten fingers. A computer's switches suggest base 2. Programmers also use base 16 (hexadecimal) and sometimes base 8 (octal) because they are compact ways to write long binary patterns. All of these are the same idea, a positional number system, and once you see the pattern, converting between them is a skill you can do in your head for small numbers.

This lesson follows Bits and Bytes. You will learn how positions carry weight, convert by hand and in code, see why hex is so common (colors, memory addresses), and learn how a computer stores negative numbers with two's complement and what happens when a number gets too large (overflow).

Positional notation

In the decimal number 472, each digit has a weight that is a power of ten: 4 x 100 + 7 x 10 + 2 x 1. The base is 10, and the weights grow by a factor of 10 to the left.

Binary works the same way with base 2. The weights are 1, 2, 4, 8, 16, 32, 64, 128 and so on. The binary number 1011 means:

1 x 8 + 0 x 4 + 1 x 2 + 1 x 1 = 11

flowchart LR
  subgraph W["Weights"]
    A["8"] --- B["4"] --- C["2"] --- D["1"]
  end
  subgraph Bits["Bits of 1011"]
    E["1"] --- F["0"] --- G["1"] --- H["1"]
  end
  A -.-> E
  B -.-> F
  C -.-> G
  D -.-> H

In 1011, the bits switch on the weights 8, 2 and 1, giving 11.

Binary to decimal

Add up the weights of the positions that contain a 1. For 1101 0110 you get 128 + 64 + 16 + 4 + 2 = 214.

Decimal to binary

Two easy methods:

  1. Subtract weights. For 37: 32 fits (remainder 5), 16 does not, 8 does not, 4 fits (remainder 1), 2 does not, 1 fits. Pattern: 100101.
  2. Divide by 2. Repeatedly divide by 2 and write down the remainders, then read them backwards. For 37: 37 / 2 = 18 r 1, 18 / 2 = 9 r 0, 9 / 2 = 4 r 1, 4 / 2 = 2 r 0, 2 / 2 = 1 r 0, 1 / 2 = 0 r 1. Reading from the last remainder up gives 100101.

Hexadecimal and octal

Long binary strings are hard to read. Hexadecimal (base 16) fixes that. It uses 16 symbols: the digits 0 to 9 and the letters A to F for the values 10 to 15. The key fact is that one hex digit equals exactly four bits, so you can convert by grouping bits in fours:

1101 0110 -> D 6 -> D6

Binary Hex Decimal
0000 0 0
0101 5 5
1001 9 9
1010 A 10
1111 F 15

One byte is always exactly two hex digits (00 to FF), which makes hex ideal for showing raw memory and colors. In web design #FF8800 is three bytes: red FF (255), green 88 (136), blue 00 (0). You will see this again in Images, Sound and Video as Data.

Octal (base 8) groups bits in threes: 101 110 -> 5 6 -> 56. It is less common today, but you still meet it in Unix file permissions such as 755, which you can explore in the Linux tutorial.

Programming languages mark the base with a prefix: 0b1011 (binary), 0o755 (octal), 0xFF (hex). A leading zero alone, as in 010, means something different in different languages, so avoid it.

JS Base converters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The loop in toBase is the "divide by 2" method generalized to any base. fromBase uses a trick called Horner's rule: multiply the running value by the base before adding each new digit, which is the same as applying the weights without computing powers.

Negative numbers: two's complement

A byte has no minus sign, only eight bits. How do we store -5? Computers use two's complement. The idea: reserve the top bit as a "sign" indicator and make the arithmetic work naturally.

For an 8-bit signed number:

  • Values from 00000000 (0) to 01111111 (127) are the non-negative numbers.
  • A pattern with the top bit set is negative. Its value is the unsigned value minus 256. So 11111111 is 255 - 256 = -1, and 10000000 is 128 - 256 = -128.

To negate a number: flip all bits, then add 1. For 5 (00000101): flip to 11111010, add 1 to get 11111011, which is -5.

Why this scheme? Because addition needs no special case. Add 5 and -5 in binary and you get 1 00000000; the leftmost carry falls off the 8-bit space and the result is 00000000, which is 0. The same adding circuit works for positive and negative numbers.

The range of a signed n-bit number is -2^(n-1) to 2^(n-1) - 1. For 8 bits that is -128 to 127. For 32 bits it is about -2.1 billion to 2.1 billion.

Overflow

If a result does not fit into the available bits, it overflows. With 8-bit unsigned numbers, 250 + 10 = 260 does not fit; the hardware keeps only the low 8 bits and gives 4. With signed numbers, 127 + 1 produces -128. This is called wrap-around.

Overflow has caused real failures in software, such as counters that suddenly go negative. Many languages protect you (Python integers grow without limit), while others wrap silently (C, Java, Rust in release mode).

JS Two's complement and overflow in 8 bits

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first table shows that patterns from 128 to 255 are the negative numbers when read as signed. JavaScript numbers are not really 8-bit, so the code uses a mask (& MASK) to simulate an 8-bit register. That is the stand-in; real hardware has the width built in.

Programs that show or parse numbers must agree on whether a pattern is signed or unsigned. The bits 11111111 mean 255 to one program and -1 to another. Many security bugs come from confusing the two.

Quick mental tricks

  • Powers of two to memorize: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024.
  • A binary number ending in 0 is even; ending in 1 is odd.
  • Shifting left by one place doubles a binary number; shifting right halves it (dropping the remainder).
  • FF is 255, 100 in hex is 256, FFFF is 65,535.
  • A hex digit is 4 bits, so a 32-bit value is 8 hex digits and a 64-bit value is 16.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the decimal value of the binary number 101010?

    32 + 8 + 2 = 42. The bits set are in the positions with weights 32, 8 and 2.
  2. 2. Why is hexadecimal popular with programmers?

    Because 16 is 2^4, each hex digit maps to a group of four bits, and a byte is two hex digits.
  3. 3. What is the 8-bit two's complement pattern for -1?

    Flip 00000001 to get 11111110, then add 1 to get 11111111.
  4. 4. In 8-bit signed arithmetic, what is 127 + 1?

    127 is 01111111. Adding 1 gives 10000000, which as a signed 8-bit pattern is -128 (overflow wraps around).
  5. 5. Which of these is the hex value of decimal 255?

    255 = 15 x 16 + 15, and 15 is F, so the value is FF.

Summary

  • Positional systems give each digit a weight that is a power of the base.
  • Binary to decimal: add the weights of the 1 bits. Decimal to binary: subtract weights or divide by 2 and read the remainders backwards.
  • One hex digit is four bits, so hex is a compact notation for binary; octal groups bits in threes.
  • Two's complement stores negatives by flipping bits and adding 1; adding works the same for signed and unsigned values.
  • A signed n-bit range is -2^(n-1) to 2^(n-1) - 1; results beyond it overflow and wrap around.
  • The same bit pattern can mean different numbers depending on signed or unsigned interpretation.

Next: how letters and symbols become numbers in Text and Character Encoding.