Guides Data & Encoding

How Number Bases Work: Binary, Octal and Hex

Place value in binary, octal and hexadecimal, converting 202 between all of them step by step, hex colours, bit operations and two's complement.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page
On this page

A number base (or radix) is how many distinct digits a number system uses before it carries over to the next column. We count in base 10 because we have ten fingers; computers use base 2 because a wire or transistor is either on or off. Octal (base 8) and hexadecimal (base 16) are shorthand for binary that people can read. Convert between them with the Number Base Converter.

Place value works the same in every base

In decimal, 202 means 2 × 100 + 0 × 10 + 2 × 1: each column is worth ten times the one on its right. In binary, each column is worth two times the one on its right, so the columns are 1, 2, 4, 8, 16, 32, 64, 128. Here is 202 written in binary:

Position 128 64 32 16 8 4 2 1
Bit of 202 1 1 0 0 1 0 1 0

Add the columns that hold a 1: 128 + 64 + 8 + 2 = 202. So decimal 202 is binary 11001010.

Converting decimal to binary

Divide by 2 repeatedly and write down the remainders; read them from the last to the first:

Division Quotient Remainder (bit)
202 ÷ 2 101 0
101 ÷ 2 50 1
50 ÷ 2 25 0
25 ÷ 2 12 1
12 ÷ 2 6 0
6 ÷ 2 3 0
3 ÷ 2 1 1
1 ÷ 2 0 1

Reading the remainders from bottom to top gives 11001010. Converting the other way, from binary to decimal, is the column sum above (try the Binary to Decimal Converter and Decimal to Binary Converter converters).

Hexadecimal: binary made readable

Hex uses 16 digits: 0-9 and then A-F for 10-15. One hex digit is exactly four bits, so converting is a matter of grouping:

Hex Decimal Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

Split 11001010 into groups of four, 1100 and 1010, and look each up: C and A. So 11001010 is CA in hex, and 202 is 0xCA. A byte (8 bits) is always exactly two hex digits, which is why hex appears in memory dumps, MAC addresses, file signatures and colour codes. See the HEX to Decimal Converter and Decimal to HEX Converter converters.

Octal groups bits in threes: 11 001 010 becomes 3 1 2, so 202 is 312 in octal. It survives mostly in Unix file permissions (chmod and File Permissions Cheat Sheet).

Hex colours

A web colour such as #FF8800 is three bytes: red FF = 255, green 88 = 136, blue 00 = 0. Each channel runs from 00 (0) to FF (255), which is 256 levels; 256 × 256 × 256 gives about 16.7 million colours. Convert colour codes with the HEX to RGB Converter.

Bit operations

Programs often work on individual bits. These are the common operations on 202:

Operation Binary Result
202 AND 15: keep the low 4 bits 11001010 → 00001010 10 (A)
202 >> 4: shift right 4 places 11001010 → 00001100 12 (C)
202 XOR 255: flip every bit 11001010 → 00110101 53 (35)

Masks like AND 15 extract part of a value, and shifts multiply or divide by powers of two.

Negative numbers

A byte can be read as unsigned (0 to 255) or signed (-128 to 127). Signed numbers usually use two's complement, where the top bit counts as -128. The same bits 11001010 are 202 unsigned and -54 signed (202 - 256). That is why overflowing a byte can flip a positive value to a negative one.

Quick reference

  • Decimal digits 0-9, binary 0-1, octal 0-7, hex 0-9 and A-F.
  • In code, prefixes mark the base: 0b binary, 0o octal, 0x hex (0b11001010, 0o312, 0xCA all equal 202).
  • 8 bits = 1 byte = 2 hex digits = values 0 to 255. Powers of two to remember: 2^8 = 256, 2^10 = 1,024, 2^16 = 65,536.

Do arithmetic directly in binary or hex with the Binary Calculator and Hex Calculator. Related terms: Binary, Hexadecimal and ASCII.

Try these tools

See also

  • Glossary Binary
    Binary is the base-2 number system, using only the digits 0 and 1, which computers use because electronic circuits are either off or on.
  • Glossary Hexadecimal
    Hexadecimal (hex) is the base-16 number system, using digits 0-9 and letters A-F, widely used as a compact way to write binary data.
  • Glossary ASCII
    ASCII is a 7-bit character encoding that assigns numbers 0 to 127 to English letters, digits, punctuation and control codes.
  • Cheat sheet ASCII Table Cheat Sheet
    All 95 printable ASCII characters with decimal and hex codes, the control characters you will meet.
  • Cheat sheet chmod and File Permissions Cheat Sheet
    Octal and symbolic chmod modes, what each digit means, how to read ls -l, special bits and umask, on one page.

Frequently Asked Questions

One hex digit is exactly four bits, so a byte is always two hex digits. It is a compact, readable way to write binary data.

AdSense Placeholder
Slot: footer_leaderboard