← Back to Computer Science

Binary and number systems

Computers represent everything with just two symbols, 0 and 1. Understanding binary is the key to understanding how computers store data.

Why binary

Computer hardware uses switches, transistors, that are either off or on, naturally representing 0 and 1. Two states are easy to distinguish reliably even with electrical noise. A single binary digit is a bit; 8 bits make a byte, which can represent 256 different values (2^8).

Place value in binary

Like decimal, binary uses place value, but each position is a power of 2 instead of 10: from the right, 1, 2, 4, 8, 16, 32, 64, 128. The binary number 1011 means 8 + 0 + 2 + 1 = 11 in decimal.

Converting decimal to binary

To convert decimal to binary, repeatedly divide by 2 and record remainders, reading them from bottom to top; or subtract the largest powers of 2. For 13: 8 fits (remainder 5), 4 fits (remainder 1), 2 does not, 1 fits, giving 1101.

Hexadecimal

Hexadecimal (base 16) uses digits 0-9 and letters A-F (10-15). Each hex digit represents exactly 4 bits, so it is a compact way to write binary: binary 1111 1010 is hex FA (15 x 16 + 10 = 250). Programmers use hex for memory addresses and colour codes, such as #FF0000 for red.

Binary arithmetic

Binary addition follows simple rules: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (0, carry 1), 1 + 1 + 1 = 11 (1, carry 1). Adding 0101 (5) and 0011 (3) gives 1000 (8). Computer processors perform arithmetic using circuits built from logic gates that follow these rules.

Negative numbers

Computers usually represent negative integers using two's complement: to negate a number, invert all its bits and add 1. In 8 bits, 5 is 00000101 and -5 is 11111011. Two's complement lets the same addition circuits handle positive and negative numbers. An 8-bit signed integer ranges from -128 to 127.

Overflow and units

Fixed-size numbers can overflow: adding 1 to the largest 8-bit unsigned value, 255, wraps around to 0. Storage is measured in kilobytes, megabytes, gigabytes, and terabytes; strictly, a kibibyte is 1,024 bytes and a kilobyte is 1,000 bytes, though the terms are often mixed up.

Worked example

Convert 45 to binary: 32 fits (13 left), 16 does not, 8 fits (5 left), 4 fits (1 left), 2 does not, 1 fits. So 45 = 101101. In hex, group from the right into 4 bits: 0010 1101 = 2D. Check: 2 x 16 + 13 = 45.

Key takeaways

Computers use binary because transistors have two states; 8 bits make a byte with 256 values. Binary place values are powers of 2. Convert decimal by subtracting powers of 2 or dividing by 2. Hexadecimal compactly represents 4 bits per digit. Binary addition uses carries. Two's complement represents negative numbers; 8-bit signed integers range from -128 to 127. Fixed sizes can overflow.

Flashcard

Bit

Binary digit, 0 or 1

Smallest unit.

Check your understanding

What is the binary number 110010 in decimal?

Take the unit quiz

← Previous lesson Next lesson →