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:
Power | Value | Binary Position | Memory/Data Size |
---|---|---|---|
2โฐ | 1 | Ones | 1 bit |
2ยน | 2 | Twos | |
2ยฒ | 4 | Fours | |
2ยณ | 8 | Eights | 1 byte (8 bits) |
2โด | 16 | Sixteens | |
2โต | 32 | Thirty-twos | |
2โถ | 64 | Sixty-fours | |
2โท | 128 | One-twenty-eights | |
2โธ | 256 | Common byte values | |
2ยนโฐ | 1,024 | 1 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.