Skip to main content

Binary Number System: Understanding Computer Language

What is Binary?โ€‹

Binary is the fundamental language that all computers use to process, store, and transmit information. While humans naturally think in decimal (base-10) using digits 0-9, computers operate entirely in binary (base-2) using only two digits: 0 and 1.

Think of binary like a light switch - it can only be ON (1) or OFF (0). This perfectly matches how computer circuits work: electricity is either flowing (1) or not flowing (0).

Why Do Computers Use Binary?โ€‹

The Hardware Realityโ€‹

Computers are built from millions of tiny electronic switches called transistors. Each transistor can be in one of two states:

  • ON (1): Electricity flows through
  • OFF (0): No electricity flows
Real-world analogy:
๐Ÿ”ฆ Flashlight ON = 1 (binary digit)
๐Ÿ”ฆ Flashlight OFF = 0 (binary digit)

A computer with 8 transistors = 8 light switches = 8 binary digits

Why Not Decimal in Hardware?โ€‹

Decimal would require 10 different voltage levels:

  • 0 volts = 0
  • 1 volt = 1
  • 2 volts = 2
  • ...and so on

This is extremely difficult and unreliable because:

  • Electrical noise could change 2.1 volts to 2.9 volts
  • Manufacturing precision would be nearly impossible
  • Circuits would be much more complex and error-prone

Binary only needs 2 voltage levels:

  • Low voltage (0-2 volts) = 0
  • High voltage (3-5 volts) = 1

This gives a large margin for error and makes circuits simple and reliable.

Understanding Binary Positionsโ€‹

Just like decimal uses powers of 10, binary uses powers of 2.

Decimal System Reminderโ€‹

Number: 347
Positions: 3ร—10ยฒ + 4ร—10ยน + 7ร—10โฐ
= 3ร—100 + 4ร—10 + 7ร—1
= 300 + 40 + 7
= 347

Binary System Works the Same Wayโ€‹

Binary: 1011
Positions: 1ร—2ยณ + 0ร—2ยฒ + 1ร—2ยน + 1ร—2โฐ
= 1ร—8 + 0ร—4 + 1ร—2 + 1ร—1
= 8 + 0 + 2 + 1
= 11 (in decimal)

Therefore: 1011โ‚‚ = 11โ‚โ‚€

Powers of 2 Reference Tableโ€‹

Understanding powers of 2 is crucial for working with binary:

PowerValueBinary PositionMemory/Data Size
2โฐ1Ones1 bit
2ยน2Twos
2ยฒ4Fours
2ยณ8Eights1 byte (8 bits)
2โด16Sixteens
2โต32Thirty-twos
2โถ64Sixty-fours
2โท128One-twenty-eights
2โธ256Common byte values
2ยนโฐ1,0241 Kilobyte (KB)

Step-by-Step Binary to Decimal Conversionโ€‹

Example 1: Convert 1101โ‚‚ to Decimalโ€‹

Binary: 1 1 0 1

Step 1: Write the powers of 2 from right to left
Position: 3 2 1 0
Power: 2ยณ 2ยฒ 2ยน 2โฐ
Value: 8 4 2 1

Step 2: Multiply each binary digit by its position value
1 ร— 8 = 8
1 ร— 4 = 4
0 ร— 2 = 0
1 ร— 1 = 1

Step 3: Add all the results
8 + 4 + 0 + 1 = 13

Therefore: 1101โ‚‚ = 13โ‚โ‚€

Example 2: Convert 10110101โ‚‚ to Decimalโ€‹

Binary: 1 0 1 1 0 1 0 1

Positions: 7 6 5 4 3 2 1 0
Powers: 2โท 2โถ 2โต 2โด 2ยณ 2ยฒ 2ยน 2โฐ
Values: 128 64 32 16 8 4 2 1

Calculation:
1 ร— 128 = 128
0 ร— 64 = 0
1 ร— 32 = 32
1 ร— 16 = 16
0 ร— 8 = 0
1 ร— 4 = 4
0 ร— 2 = 0
1 ร— 1 = 1

Total: 128 + 0 + 32 + 16 + 0 + 4 + 0 + 1 = 181

Therefore: 10110101โ‚‚ = 181โ‚โ‚€

Step-by-Step Decimal to Binary Conversionโ€‹

Method: Repeated Division by 2โ€‹

The process is simple: divide by 2, record the remainder, repeat until you reach 0.

Example 1: Convert 13โ‚โ‚€ to Binaryโ€‹

Step 1: Divide 13 by 2
13 รท 2 = 6 remainder 1 โ† This is the rightmost binary digit

Step 2: Divide 6 by 2
6 รท 2 = 3 remainder 0

Step 3: Divide 3 by 2
3 รท 2 = 1 remainder 1

Step 4: Divide 1 by 2
1 รท 2 = 0 remainder 1 โ† This is the leftmost binary digit

Step 5: Read remainders from bottom to top
13โ‚โ‚€ = 1101โ‚‚

Verification: 1ร—8 + 1ร—4 + 0ร—2 + 1ร—1 = 8 + 4 + 0 + 1 = 13 โœ“

Example 2: Convert 156โ‚โ‚€ to Binaryโ€‹

156 รท 2 = 78 remainder 0  โ† Rightmost digit
78 รท 2 = 39 remainder 0
39 รท 2 = 19 remainder 1
19 รท 2 = 9 remainder 1
9 รท 2 = 4 remainder 1
4 รท 2 = 2 remainder 0
2 รท 2 = 1 remainder 0
1 รท 2 = 0 remainder 1 โ† Leftmost digit

Reading from bottom to top: 156โ‚โ‚€ = 10011100โ‚‚

Verification:
1ร—128 + 0ร—64 + 0ร—32 + 1ร—16 + 1ร—8 + 1ร—4 + 0ร—2 + 0ร—1
= 128 + 0 + 0 + 16 + 8 + 4 + 0 + 0 = 156 โœ“

Binary Arithmetic: How Computers Calculateโ€‹

Binary Addition Rulesโ€‹

Binary addition follows simple rules:

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

Example: Adding 1011โ‚‚ + 1101โ‚‚โ€‹

   1011  (11 in decimal)
+ 1101 (13 in decimal)
------
11000 (24 in decimal)

Step-by-step from right to left:
Column 1: 1 + 1 = 10 (write 0, carry 1)
Column 2: 1 + 0 + 1(carry) = 10 (write 0, carry 1)
Column 3: 0 + 1 + 1(carry) = 10 (write 0, carry 1)
Column 4: 1 + 1 + 1(carry) = 11 (write 1, carry 1)
Column 5: 0 + 0 + 1(carry) = 1

Verification: 11 + 13 = 24 โœ“

Binary Subtraction Exampleโ€‹

  1101  (13 in decimal)
- 1011 (11 in decimal)
------
0010 (2 in decimal)

Verification: 13 - 11 = 2 โœ“

Real-World Computer Applicationsโ€‹

1. Data Storage: Bits and Bytesโ€‹

Bit: The smallest unit of data - one binary digit (0 or 1) Byte: 8 bits grouped together

1 bit  = 1 binary digit (0 or 1)
1 byte = 8 bits = can represent 256 different values (0-255)

Example byte: 10110101
This byte represents the decimal number 181

Why 8 bits = 1 byte?

  • 8 bits can represent 2โธ = 256 different combinations
  • Perfect for representing ASCII characters (A-Z, 0-9, symbols)
  • Convenient size for computer memory addressing

2. ASCII Character Encodingโ€‹

Every character you type has a binary representation:

Character 'A' = 65 in decimal = 01000001 in binary
Character 'B' = 66 in decimal = 01000010 in binary
Character '0' = 48 in decimal = 00110000 in binary
Character ' ' (space) = 32 in decimal = 00100000 in binary

When you type "AB", your computer stores:
01000001 01000010

3. IP Addressesโ€‹

IPv4 addresses are 32 bits (4 bytes):

IP Address: 192.168.1.1

Breaking it down:
192 = 11000000 (binary)
168 = 10101000 (binary)
1 = 00000001 (binary)
1 = 00000001 (binary)

Full binary: 11000000.10101000.00000001.00000001

4. RGB Color Valuesโ€‹

Computer screens use Red, Green, Blue (RGB) values:

Color: Pure Red
RGB: (255, 0, 0)

In binary:
Red: 255 = 11111111 (maximum intensity)
Green: 0 = 00000000 (no green)
Blue: 0 = 00000000 (no blue)

Complete: 11111111 00000000 00000000

5. File Sizes and Memoryโ€‹

1 KB (Kilobyte) = 1,024 bytes = 2ยนโฐ bytes
1 MB (Megabyte) = 1,024 KB = 2ยฒโฐ bytes
1 GB (Gigabyte) = 1,024 MB = 2ยณโฐ bytes

Why 1,024 instead of 1,000?
Because computers use powers of 2:
2ยนโฐ = 1,024 (close to 1,000, but exact power of 2)

Bitwise Operations: How Programs Manipulate Binaryโ€‹

AND Operation (&)โ€‹

Used for masking - turning off specific bits:

Example: Check if a number is even

Number: 13 = 1101
Mask: 1 = 0001
----
Result: 1 = 0001 (1 = odd, 0 = even)

How it works:
1 & 1 = 1
1 & 0 = 0
0 & 0 = 0
1 & 1 = 1

OR Operation (|)โ€‹

Used for setting specific bits:

Example: Set the 4th bit to 1

Number: 1001 (9 in decimal)
Mask: 1000 (8 in decimal)
----
Result: 1001 (still 9, 4th bit was already 1)

If number was: 0001 (1 in decimal)
Result would be: 1001 (9 in decimal)

XOR Operation (^)โ€‹

Used for toggling bits and encryption:

Example: Simple encryption

Message: 1010 (10 in decimal)
Key: 1100 (12 in decimal)
----
Encrypted: 0110 (6 in decimal)

To decrypt, XOR again with same key:
Encrypted: 0110 (6 in decimal)
Key: 1100 (12 in decimal)
----
Original: 1010 (10 in decimal) โœ“

Common Binary Patterns to Memorizeโ€‹

Powers of 2 in Binaryโ€‹

1โ‚โ‚€   = 1โ‚‚       = 2โฐ
2โ‚โ‚€ = 10โ‚‚ = 2ยน
4โ‚โ‚€ = 100โ‚‚ = 2ยฒ
8โ‚โ‚€ = 1000โ‚‚ = 2ยณ
16โ‚โ‚€ = 10000โ‚‚ = 2โด
32โ‚โ‚€ = 100000โ‚‚ = 2โต
64โ‚โ‚€ = 1000000โ‚‚ = 2โถ
128โ‚โ‚€ = 10000000โ‚‚ = 2โท
256โ‚โ‚€ = 100000000โ‚‚ = 2โธ

Common Decimal Numbersโ€‹

10โ‚โ‚€  = 1010โ‚‚
15โ‚โ‚€ = 1111โ‚‚ (all 4 bits set)
31โ‚โ‚€ = 11111โ‚‚ (all 5 bits set)
63โ‚โ‚€ = 111111โ‚‚ (all 6 bits set)
127โ‚โ‚€ = 1111111โ‚‚ (all 7 bits set)
255โ‚โ‚€ = 11111111โ‚‚ (all 8 bits set - maximum byte value)

Troubleshooting Common Mistakesโ€‹

Mistake 1: Reading Binary Positions Wrongโ€‹

โŒ Wrong: Reading 1011 as "one thousand eleven"
โœ… Correct: Reading 1011 as "one-zero-one-one" = 11 in decimal

Binary digits are positions, not thousands/hundreds like decimal!

Mistake 2: Forgetting to Carry in Additionโ€‹

โŒ Wrong:    โœ… Correct:
1011 1011
+ 1101 + 1101
------ ------
2020 11000

Mistake 3: Confusing Binary with Decimalโ€‹

โŒ Wrong: 101โ‚‚ + 10โ‚‚ = 111โ‚‚
โŒ This treats binary like decimal addition

โœ… Correct: 101โ‚‚ + 10โ‚‚ = 111โ‚‚
โœ… But verify: (5โ‚โ‚€) + (2โ‚โ‚€) = (7โ‚โ‚€)
โœ… And 111โ‚‚ = 7โ‚โ‚€ โœ“

Programming Examplesโ€‹

JavaScript Binary Operationsโ€‹

// Converting between binary and decimal
let decimal = 13;
let binary = decimal.toString(2); // "1101"
console.log(`${decimal} in binary: ${binary}`);

let binaryString = "1101";
let backToDecimal = parseInt(binaryString, 2); // 13
console.log(`${binaryString} in decimal: ${backToDecimal}`);

// Bitwise operations
let a = 12; // 1100 in binary
let b = 10; // 1010 in binary

console.log(`${a} & ${b} = ${a & b}`); // 8 (1000)
console.log(`${a} | ${b} = ${a | b}`); // 14 (1110)
console.log(`${a} ^ ${b} = ${a ^ b}`); // 6 (0110)

// Checking if number is even/odd
function isEven(num) {
return (num & 1) === 0; // Check if last bit is 0
}

console.log(isEven(12)); // true (1100 & 0001 = 0000)
console.log(isEven(13)); // false (1101 & 0001 = 0001)

Python Binary Operationsโ€‹

# Converting between binary and decimal
decimal = 13
binary = bin(decimal) # '0b1101'
binary_no_prefix = binary[2:] # '1101'
print(f"{decimal} in binary: {binary_no_prefix}")

binary_string = "1101"
back_to_decimal = int(binary_string, 2) # 13
print(f"{binary_string} in decimal: {back_to_decimal}")

# Bitwise operations
a = 12 # 1100 in binary
b = 10 # 1010 in binary

print(f"{a} & {b} = {a & b}") # 8
print(f"{a} | {b} = {a | b}") # 14
print(f"{a} ^ {b} = {a ^ b}") # 6

# Working with individual bits
def get_bit(number, position):
"""Get the bit at a specific position (0 = rightmost)"""
return (number >> position) & 1

def set_bit(number, position):
"""Set the bit at a specific position to 1"""
return number | (1 << position)

def clear_bit(number, position):
"""Set the bit at a specific position to 0"""
return number & ~(1 << position)

# Examples
num = 12 # 1100 in binary
print(f"Bit 2 of {num}: {get_bit(num, 2)}") # 1
print(f"Set bit 0 of {num}: {set_bit(num, 0)}") # 13 (1101)
print(f"Clear bit 3 of {num}: {clear_bit(num, 3)}") # 4 (0100)

Key Takeawaysโ€‹

โœ… Binary is the foundation of all computing - every operation starts here
โœ… Only two digits (0, 1) map perfectly to electronic switches
โœ… Each position represents a power of 2 (1, 2, 4, 8, 16, 32, 64, 128...)
โœ… 1 byte = 8 bits can represent values 0-255
โœ… Bitwise operations are fundamental to programming and optimization
โœ… Everything digital - text, images, videos, programs - is stored as binary

Understanding binary gives you insight into:

  • How computers actually work at the hardware level
  • Why certain numbers appear frequently in computing (powers of 2)
  • How data compression and encryption work
  • Why computer memory is measured in 1024s instead of 1000s
  • How to optimize code using bitwise operations

Binary isn't just an academic concept - it's the actual language your computer uses for every operation, from displaying this text to running complex algorithms.