Chapter 24: Binary Numbers

Part 1: What Are Binary Numbers?

Binary numbers are numbers expressed in the base-2 numeral system. Unlike our everyday decimal system (base-10), which uses ten digits (0-9), binary uses only two digits: 0 and 1.

Why only two digits? Because computers are made of millions of tiny electronic switches called transistors. These switches can only be in one of two states:

  • OFF (representing 0)

  • ON (representing 1)

This simple on/off system is incredibly reliable and forms the foundation of all digital computing.

Part 2: Understanding Place Value in Binary

In our familiar decimal system, each digit’s place value is a power of 10:

  • 3,472 means (3 × 10³) + (4 × 10²) + (7 × 10¹) + (2 × 10⁰)

In binary, each digit’s place value is a power of 2:

  • The rightmost digit (least significant bit) is the 1’s place (2⁰)

  • The next digit is the 2’s place (2¹)

  • Then the 4’s place (2²)

  • Then the 8’s place (2³)

  • And so on…

Let’s look at the place values for an 8-bit binary number (a byte):

Bit Position 7 6 5 4 3 2 1 0
Place Value 2⁷ 2⁶ 2⁵ 2⁴ 2⁰
Decimal Value 128 64 32 16 8 4 2 1

Part 3: Converting Binary to Decimal

To convert a binary number to decimal, simply add up the place values wherever there’s a 1.

Example 1: Convert binary 1011 to decimal.

Binary 1 0 1 1
Place Value 2³ = 8 2² = 4 2¹ = 2 2⁰ = 1
Contribution 8 0 2 1

Total: 8 + 0 + 2 + 1 = 11

So binary 1011 = decimal 11.

Example 2: Convert binary 11010 to decimal.

Binary 1 1 0 1 0
Place Value 2⁴ = 16 2³ = 8 2² = 4 2¹ = 2 2⁰ = 1
Contribution 16 8 0 2 0

Total: 16 + 8 + 0 + 2 + 0 = 26

So binary 11010 = decimal 26.

In Python:

python

Part 4: Converting Decimal to Binary

To convert a decimal number to binary, repeatedly divide by 2 and collect the remainders from bottom to top.

Example: Convert decimal 25 to binary.

Division Quotient Remainder
25 ÷ 2 12 1 (least significant bit)
12 ÷ 2 6 0
6 ÷ 2 3 0
3 ÷ 2 1 1
1 ÷ 2 0 1 (most significant bit)

Reading the remainders from bottom to top: 11001

So decimal 25 = binary 11001.

Let’s verify: (1 × 16) + (1 × 8) + (0 × 4) + (0 × 2) + (1 × 1) = 16 + 8 + 0 + 0 + 1 = 25 ✓

In Python:

python

Part 5: Binary Addition

Adding binary numbers is similar to decimal addition, but you carry over when you reach 2 instead of 10.

Rules of binary addition:

  • 0 + 0 = 0

  • 0 + 1 = 1

  • 1 + 0 = 1

  • 1 + 1 = 0, carry 1 (since 2 in binary is 10)

  • 1 + 1 + 1 = 1, carry 1 (since 3 in binary is 11)

Example: Add binary 1011 (11) and 1101 (13).

text

Let’s add from right to left:

  1. Rightmost column: 1 + 1 = 0, carry 1

  2. Next column: 1 (carry) + 1 + 0 = 0, carry 1

  3. Next column: 1 (carry) + 0 + 1 = 0, carry 1

  4. Leftmost column: 1 (carry) + 1 + 1 = 1, carry 1

  5. Final carry: 1

Result: 11000 (which is 24)

text

In Python:

python

Part 6: Binary Subtraction

Binary subtraction uses borrowing, similar to decimal subtraction.

Rules:

  • 0 – 0 = 0

  • 1 – 0 = 1

  • 1 – 1 = 0

  • 0 – 1 = 1, borrow 1 from the next column

Example: Subtract binary 101 (5) from 1001 (9).

text

  1. Rightmost: 1 – 1 = 0

  2. Next column: 0 – 0 = 0

  3. Next column: 0 – 1 → need to borrow. Borrow from the leftmost 1, making it 0, and this column becomes 10 (2 in decimal). 10 – 1 = 1

  4. Leftmost: 0 (after borrowing) – 0 = 0

Result: 0100 (which is 4)

text

Part 7: Binary in Programming

Representing Binary in Code

Most programming languages have a way to write binary literals directly:

python

Common Binary Operations

python

Part 8: Practical Applications of Binary

1. Bit Flags (Permissions)

Binary is perfect for representing sets of true/false flags. Each bit can represent a single permission.

python

2. Color Representation

In web development, colors are often represented in hexadecimal (base-16), which is closely related to binary.

python

3. Checking if a Number is Even or Odd

The fastest way to check if a number is even or odd is to look at its least significant bit (the rightmost bit).

python

4. Multiplying and Dividing by Powers of 2

Left shift (<<) multiplies by 2; right shift (>>) divides by 2 (floor division).

python

Part 9: Hexadecimal and Octal

Binary numbers can get very long and hard to read. Programmers often use hexadecimal (base-16) and octal (base-8) as shorthand.

Hexadecimal (Base-16)

Uses digits 0-9 and letters A-F (where A=10, B=11, C=12, D=13, E=14, F=15).

  • One hexadecimal digit represents exactly 4 binary digits (a nibble).

  • 0xFF is 255 in decimal, 11111111 in binary.

  • 0xA5 is 165 in decimal, 10100101 in binary.

python

Octal (Base-8)

Uses digits 0-7. Each octal digit represents 3 binary digits.

python

Summary: The Binary Philosophy

  • Binary is a base-2 number system using only 0 and 1.

  • It’s the native language of computers because they’re built from two-state electronic switches.

  • Each binary digit is a bit.

  • Place values in binary are powers of 2 (1, 2, 4, 8, 16, 32, 64, 128…).

  • Converting binary to decimal: add place values where bits are 1.

  • Converting decimal to binary: repeatedly divide by 2 and read remainders upward.

  • Binary arithmetic (addition, subtraction) works like decimal but with carries at 2.

  • Bitwise operators (&|^~<<>>) manipulate individual bits.

  • Hexadecimal (base-16) and octal (base-8) are convenient shorthand for binary.

Understanding binary is like learning the alphabet of the digital world. It’s the foundation upon which all of computing is built. Once you grasp how computers see numbers, you’ll have a much deeper appreciation for how they handle everything else—text, images, sound, and more.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *