Binary Number System Explained

The Base-2 Number System

Binary is a base-2 number system that uses only two digits: 0 and 1. It is the fundamental language of computers — every piece of data stored or processed by a computer is ultimately represented as sequences of binary digits. Understanding binary is essential for anyone working in computing, from software developers to network engineers to cybersecurity professionals.

In a positional number system, each digit's position represents a power of the base. In decimal (base-10), the number 1011 means 1×10³ + 0×10² + 1×10¹ + 1×10⁰ = 1000 + 0 + 10 + 1 = 1011. In binary (base-2), the number 1011 means 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 8 + 0 + 2 + 1 = 11 in decimal.

The binary system was formalized by Gottfried Wilhelm Leibniz in 1679, who recognized its philosophical connection to the I Ching's hexagrams. However, it was not practically applied until the 20th century, when electronic computers needed a reliable way to represent data using switches that could be either on (1) or off (0).

Binary's simplicity is its strength. With only two states, binary digits are extremely easy to represent physically: a voltage above a threshold is 1, below is 0; a magnetized region is 1, unmagnetized is 0; a pit on a CD is 1, a land is 0. This binary representation is robust against noise and degradation, making it reliable for data storage and transmission.

Each binary digit is called a bit (binary digit). A group of 8 bits is called a byte. Larger groups include nibble (4 bits), word (16 or 32 bits depending on the architecture), and double word (32 or 64 bits). These groupings form the foundation of computer data organization.

Bits and Bytes

A bit is the smallest unit of data in computing. It can hold one of two values: 0 or 1. A single bit can represent a boolean state (true/false, yes/no, on/off), but cannot represent more complex information alone. By combining multiple bits, we can represent increasingly larger ranges of values.

Unit Bits Values Typical Use
Bit12 (0 or 1)Boolean flag, single switch
Nibble416 (0-15)Single hex digit, BCD
Byte8256 (0-255)ASCII character, pixel channel
Word1665,536UTF-16 character, short integer
Double Word32~4.3 billionIPv4 address, integer, float
Quad Word64~1.8 × 10¹⁹IPv6 address, pointer, timestamp

The byte is the fundamental addressable unit in most computer architectures. When you say a file is 1 megabyte, it contains approximately 1 million bytes (8 million bits). Memory addresses typically point to individual bytes, not bits, which is why byte counts are more common than bit counts in computing.

With n bits, you can represent 2ⁿ distinct values. This exponential relationship is fundamental to computing: 8 bits give 256 values, 16 bits give 65,536 values, 32 bits give about 4.3 billion values. This is why 32-bit systems can only address 4 GB of memory (2³² bytes) and why IPv4 addresses (32 bits) are running out — there are only about 4.3 billion possible addresses.

Bit numbering starts from 0 on the right (least significant bit, LSB). In the byte 10110010, bit 0 is 0 (rightmost), bit 1 is 1, bit 2 is 0, and so on up to bit 7 which is 1 (leftmost, most significant bit or MSB). This right-to-left numbering matches the positional value system where bit n has value 2ⁿ.

How Computers Use Binary

Computers use binary because electronic circuits are most reliable when they have only two distinct states. A transistor — the fundamental building block of processors — acts as a switch that is either conducting (on/1) or not conducting (off/0). By combining billions of transistors, computers perform complex operations using nothing but these simple binary decisions.

All data in a computer is represented in binary:

  • Text: Each character is mapped to a number via encoding standards. ASCII uses 7 bits (1 byte) per character. UTF-8 uses 1-4 bytes per character. The letter 'A' is 01000001 (65 in decimal).
  • Images: Each pixel's color is stored as binary numbers. In 24-bit color, each pixel uses 3 bytes: one byte each for red, green, and blue intensity (0-255).
  • Sound: Audio is sampled thousands of times per second, with each sample stored as a binary number. CD-quality audio uses 16-bit samples at 44,100 samples per second.
  • Programs: Machine code instructions are binary numbers that the processor directly executes. Each instruction is an opcode followed by operands, all in binary.
  • Floating-point numbers: Real numbers are stored using the IEEE 754 standard, which uses binary for the sign bit, exponent, and mantissa.

Logic gates — AND, OR, NOT, XOR, NAND, NOR — are the physical building blocks that process binary data. An AND gate outputs 1 only if both inputs are 1. An OR gate outputs 1 if either input is 1. A NOT gate flips the input. By combining these simple gates, computers build adders, multipliers, comparators, and ultimately complete processors.

The CPU fetches binary instructions from memory, decodes them into operations, executes them using the arithmetic logic unit (ALU), and stores the results back in memory or registers. Every operation — from displaying a web page to running a video game — ultimately reduces to billions of binary operations per second.

Converting Binary to Decimal

Converting binary to decimal uses the positional value method. Each bit is multiplied by 2 raised to the power of its position (counting from right to left, starting at 0), and the results are summed.

The general formula for a binary number with bits bₙbₙ₋₁...b₁b₀ is:

Decimal = bₙ × 2ⁿ + bₙ₋₁ × 2ⁿ⁻¹ + ... + b₁ × 2¹ + b₀ × 2⁰

Example 1: Convert 1101 to decimal

  • Bit 3 (1): 1 × 8 = 8
  • Bit 2 (1): 1 × 4 = 4
  • Bit 1 (0): 0 × 2 = 0
  • Bit 0 (1): 1 × 1 = 1
  • Sum: 8 + 4 + 0 + 1 = 13
  • Therefore, 1101₂ = 13₁₀

Example 2: Convert 10110010 to decimal

  • Bit 7 (1): 1 × 128 = 128
  • Bit 6 (0): 0 × 64 = 0
  • Bit 5 (1): 1 × 32 = 32
  • Bit 4 (1): 1 × 16 = 16
  • Bit 3 (0): 0 × 8 = 0
  • Bit 2 (0): 0 × 4 = 0
  • Bit 1 (1): 1 × 2 = 2
  • Bit 0 (0): 0 × 1 = 0
  • Sum: 128 + 32 + 16 + 2 = 178
  • Therefore, 10110010₂ = 178₁₀

A shortcut: only add the positional values where the bit is 1. The positional values for an 8-bit byte are: 128, 64, 32, 16, 8, 4, 2, 1. For 10110010, the 1-bits are at positions 7, 5, 4, and 1, so the value is 128 + 32 + 16 + 2 = 178.

Converting Decimal to Binary

To convert decimal to binary, use the repeated division method: divide by 2, record the remainder, and continue with the quotient until it reaches 0. The binary number is the remainders read from bottom to top.

Example: Convert 178 to binary

  • 178 ÷ 2 = 89 remainder 0
  • 89 ÷ 2 = 44 remainder 1
  • 44 ÷ 2 = 22 remainder 0
  • 22 ÷ 2 = 11 remainder 0
  • 11 ÷ 2 = 5 remainder 1
  • 5 ÷ 2 = 2 remainder 1
  • 2 ÷ 2 = 1 remainder 0
  • 1 ÷ 2 = 0 remainder 1
  • Read remainders bottom to top: 10110010

Binary Arithmetic

Binary arithmetic follows the same principles as decimal arithmetic but uses base-2. Understanding binary addition, subtraction, multiplication, and division is fundamental to understanding how computers perform calculations.

Binary Addition

Binary addition has four simple rules:

  • 0 + 0 = 0
  • 0 + 1 = 1
  • 1 + 0 = 1
  • 1 + 1 = 10 (0 with carry 1)
  • 1 + 1 + 1 = 11 (1 with carry 1)

Example: Add 1011 + 1101

    1 1 1 1   (carries)
    1 0 1 1
  + 1 1 0 1
  ---------
  1 1 0 0 0
        

Verification: 11 + 13 = 24 = 11000₂ ✓

Binary Subtraction

Binary subtraction also has four rules:

  • 0 - 0 = 0
  • 1 - 0 = 1
  • 1 - 1 = 0
  • 0 - 1 = 1 (borrow 1 from next column)

Binary Multiplication

Binary multiplication is simpler than decimal because each digit is either 0 or 1:

  • 0 × 0 = 0
  • 0 × 1 = 0
  • 1 × 0 = 0
  • 1 × 1 = 1

Multiplication is just repeated addition with shifting. To multiply by 2, shift all bits left by one position (like multiplying by 10 in decimal). To divide by 2, shift right by one position.

Example: Multiply 101 × 11

      1 0 1
    ×   1 1
    -------
      1 0 1   (101 × 1)
    1 0 1     (101 × 1, shifted left)
    -------
    1 1 1 1
        

Verification: 5 × 3 = 15 = 1111₂ ✓

Signed and Unsigned Numbers

In computing, numbers can be unsigned (always positive) or signed (positive or negative). The interpretation of the bits changes depending on whether the number is signed or unsigned.

An unsigned 8-bit number can represent values from 0 to 255 (2⁸ - 1). All 256 possible bit patterns map to non-negative values. The binary number 11111111 represents 255, and 00000000 represents 0.

A signed 8-bit number must represent both positive and negative values. Several methods exist for representing signed numbers, but the most common is two's complement, which we will discuss in the next section.

Bits Unsigned Value Signed Value (Two's Complement)
0000000000
0000000111
0000001022
01111111127127
10000000128-128
10000001129-127
11111110254-2
11111111255-1

The MSB (most significant bit) serves as the sign bit in signed representations. If the MSB is 0, the number is non-negative. If the MSB is 1, the number is negative. This means the range of a signed n-bit number is -2ⁿ⁻¹ to 2ⁿ⁻¹ - 1, while an unsigned n-bit number ranges from 0 to 2ⁿ - 1.

Common signed integer sizes in programming:

  • 8-bit signed (int8): -128 to 127
  • 16-bit signed (int16): -32,768 to 32,767
  • 32-bit signed (int32): -2,147,483,648 to 2,147,483,647
  • 64-bit signed (int64): -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Integer overflow occurs when an arithmetic result exceeds the representable range. For example, adding 1 to the unsigned 8-bit value 255 (11111111) wraps around to 0. In signed 8-bit, adding 1 to 127 (01111111) overflows to -128 (10000000). Understanding overflow is critical for writing correct programs, especially in security-sensitive contexts where overflow bugs can lead to vulnerabilities.

Two's Complement

Two's complement is the standard method for representing signed integers in binary. It is elegant because it allows subtraction to be performed using the same circuitry as addition, simplifying processor design. Nearly all modern computers use two's complement for signed integer arithmetic.

To find the two's complement of a binary number (to negate it):

  1. Invert all bits (change 0 to 1 and 1 to 0) — this is the one's complement
  2. Add 1 to the result

Example: Find the two's complement of 5 (00000101)

  • Original: 00000101
  • One's complement (invert): 11111010
  • Add 1: 11111011
  • Result: 11111011 represents -5

Example: Find the two's complement of -3 (to get 3)

  • -3 in binary: 11111101
  • One's complement: 00000010
  • Add 1: 00000011
  • Result: 00000011 represents 3

Two's complement has several elegant properties:

  • Single zero: There is only one representation for zero (00000000), unlike one's complement which has both +0 and -0
  • Simple addition: Adding positive and negative numbers works correctly without special handling. Just add the bits and discard any overflow
  • Sign extension: To extend a signed number to more bits, copy the sign bit to all new high-order bits. -5 in 8 bits (11111011) becomes -5 in 16 bits (1111111111111011)
  • Range asymmetry: The range is -2ⁿ⁻¹ to 2ⁿ⁻¹ - 1. For 8 bits: -128 to 127. The negative range has one more value because zero takes a positive slot

Two's Complement Addition Examples

Example: 5 + (-3) = 2

    00000101   (5)
  + 11111101   (-3)
  ----------
    00000010   (2) ✓ — the carry out of bit 7 is discarded
        

Example: -5 + (-3) = -8

    11111011   (-5)
  + 11111101   (-3)
  ----------
    11111000   (-8) ✓
        

The beauty of two's complement is that the same binary addition circuit handles both positive and negative numbers without any special logic. The processor does not need to know whether numbers are signed or unsigned when performing addition — it simply adds the bits. The programmer (or compiler) is responsible for interpreting the results correctly.

Byte Units and Data Sizes

Data sizes in computing are measured in bytes and their multiples. Understanding these units is essential for working with files, memory, networks, and storage devices. There are two systems of prefixes: binary (powers of 1024) and decimal (powers of 1000).

Unit Binary (IEC) Decimal (SI) Common Usage
Byte 1 byte 1 byte Single character, small number
Kilobyte 1 KiB = 1,024 bytes 1 KB = 1,000 bytes Short document, small image
Megabyte 1 MiB = 1,048,576 bytes 1 MB = 1,000,000 bytes MP3 song, photo
Gigabyte 1 GiB = 1,073,741,824 bytes 1 GB = 1,000,000,000 bytes Movie, game installation
Terabyte 1 TiB = 1,099,511,627,776 bytes 1 TB = 1,000,000,000,000 bytes Hard drive, large database
Petabyte 1 PiB = 1,125,899,906,842,624 bytes 1 PB = 10¹⁵ bytes Data center, scientific data

The confusion between binary and decimal prefixes causes real-world problems. A hard drive marketed as 1 TB (1,000,000,000,000 bytes) appears as approximately 931 GiB in an operating system that uses binary prefixes. The drive manufacturer uses decimal (SI) prefixes, while the OS uses binary (IEC) prefixes. This discrepancy means you get about 7% less "space" than advertised.

To avoid ambiguity, the International Electrotechnical Commission (IEC) introduced binary prefixes in 1998: kibi (Ki), mebi (Mi), gibi (Gi), tebi (Ti), pebi (Pi), and exbi (Ei). These always represent exact powers of 1024. However, adoption has been slow, and most people still use "kilobyte" to mean 1024 bytes in casual conversation.

Common data sizes for reference:

  • 1 bit: A single boolean value (true/false)
  • 1 byte: A single ASCII character
  • 1 KiB: A short email or text message
  • 1 MiB: A 1-megapixel JPEG photo or a short MP3
  • 1 GiB: A DVD-quality movie or about 250 MP3 songs
  • 1 TiB: About 250,000 photos or 500 hours of video
  • 1 PiB: About 500 billion pages of text

Network speeds are typically measured in bits per second (bps), not bytes per second. A 100 Mbps (megabits per second) connection transfers about 12.5 MB/s (megabytes per second). The factor of 8 (bits to bytes) is a common source of confusion. To convert from bits to bytes, divide by 8. To convert from bytes to bits, multiply by 8.

Understanding binary is the foundation of computer science. Every concept in computing — from data types to memory addresses to network protocols to cryptographic algorithms — builds upon the binary number system. Mastering binary arithmetic and bit manipulation opens the door to deeper understanding of how computers actually work.