Every number your computer works with is stored as binary — a sequence of 0s and 1s. Understanding how binary and decimal relate is one of those foundational skills that makes a lot of other computing concepts click.
Here's how the conversion works, step by step.
What is binary?
Binary is a base-2 number system. Decimal (what humans normally use) is base-10 — it has ten digits (0–9). Binary only has two digits: 0 and 1.
Each digit in binary is called a bit (binary digit). Eight bits make a byte.
Just like decimal uses powers of 10 (ones, tens, hundreds, thousands…), binary uses powers of 2:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Binary to decimal: the algorithm
To convert a binary number to decimal, multiply each bit by its positional power of 2, then sum everything.
Example: convert 1011 to decimal
Position: 3 2 1 0
Bit: 1 0 1 1
Value: (1×8) + (0×4) + (1×2) + (1×1)
= 8 + 0 + 2 + 1
= 11
So 1011 in binary = 11 in decimal.
Example: convert 11001010 to decimal
Position: 7 6 5 4 3 2 1 0
Bit: 1 1 0 0 1 0 1 0
Value: 128 + 64 + 0 + 0 + 8 + 0 + 2 + 0
= 202
11001010 in binary = 202 in decimal.
Step-by-step method
- Write the binary number with position numbers starting at 0 on the right.
- For each bit, multiply its value (0 or 1) by 2^position.
- Add all the results together.
That's it. No special calculator needed once you memorize the powers of 2.
Powers of 2 reference table
| Power | Value |
|---|---|
| 2⁰ | 1 |
| 2¹ | 2 |
| 2² | 4 |
| 2³ | 8 |
| 2⁴ | 16 |
| 2⁵ | 32 |
| 2⁶ | 64 |
| 2⁷ | 128 |
| 2⁸ | 256 |
| 2⁹ | 512 |
| 2¹⁰ | 1024 |
Decimal to binary: the algorithm
Going the other direction (decimal → binary) uses repeated division by 2.
Example: convert 45 to binary
45 ÷ 2 = 22 remainder 1 ← least significant bit
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 ← most significant bit
Read the remainders bottom to top: 101101
Check: 32 + 8 + 4 + 1 = 45 ✓
Step-by-step method
- Divide the decimal number by 2.
- Write down the remainder (0 or 1).
- Divide the quotient by 2 again.
- Repeat until the quotient is 0.
- The binary number is the remainders read from last to first (bottom to top).
Conversion in code
JavaScript:
// Binary string to decimal
function binaryToDecimal(binary) {
return parseInt(binary, 2);
}
// Decimal to binary string
function decimalToBinary(decimal) {
return decimal.toString(2);
}
console.log(binaryToDecimal("1011")); // 11
console.log(binaryToDecimal("11001010")); // 202
console.log(decimalToBinary(45)); // "101101"
console.log(decimalToBinary(255)); // "11111111"
Python:
# Binary string to decimal
def binary_to_decimal(binary: str) -> int:
return int(binary, 2)
# Decimal to binary string (removes "0b" prefix)
def decimal_to_binary(decimal: int) -> str:
return bin(decimal)[2:]
print(binary_to_decimal("1011")) # 11
print(binary_to_decimal("11001010")) # 202
print(decimal_to_binary(45)) # "101101"
print(decimal_to_binary(255)) # "11111111"
Go:
import (
"fmt"
"strconv"
)
func BinaryToDecimal(binary string) (int64, error) {
return strconv.ParseInt(binary, 2, 64)
}
func DecimalToBinary(decimal int64) string {
return strconv.FormatInt(decimal, 2)
}
func main() {
val, _ := BinaryToDecimal("1011")
fmt.Println(val) // 11
fmt.Println(DecimalToBinary(45)) // 101101
}
PHP:
// Binary string to decimal
function binaryToDecimal(string $binary): int {
return bindec($binary);
}
// Decimal to binary string
function decimalToBinary(int $decimal): string {
return decbin($decimal);
}
echo binaryToDecimal("1011"); // 11
echo binaryToDecimal("11001010"); // 202
echo decimalToBinary(45); // 101101
echo decimalToBinary(255); // 11111111
Common binary values to memorize
| Binary | Decimal | Notes |
|---|---|---|
0000 |
0 | |
0001 |
1 | |
0010 |
2 | |
0100 |
4 | |
1000 |
8 | |
1111 |
15 | 4-bit max |
11111111 |
255 | 8-bit max (one byte) |
10000000 |
128 | MSB of a byte |
01111111 |
127 | Max signed 8-bit int |
Hex and octal: related systems
Binary is often written in hexadecimal (base 16) because one hex digit maps exactly to four bits:
| Hex | Binary | Decimal |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| A | 1010 | 10 |
| F | 1111 | 15 |
So 0xFF (hex) = 11111111 (binary) = 255 (decimal).
Hex is common in color codes (#FF5733), memory addresses, and debugging dumps. It's compact while still mapping cleanly to binary.
Octal (base 8) groups bits in threes and was popular in older Unix file permissions (chmod 755 → 111 101 101 binary).
Practical context: where you see binary
- IP addresses: under the hood,
192.168.1.1is four 8-bit binary numbers - Colors in CSS:
#FF0000(red) is three 8-bit hex values, each representable in 8 bits - File permissions: Unix
chmod 644uses octal, which maps to binary flags - Character encoding: ASCII 'A' is decimal 65, binary
01000001 - Bitwise operations: in code, operators like
&,|,^,<<,>>work directly on bits
Understanding binary makes all of these make more sense.
Convert binary online
If you need to quickly convert between binary, decimal, hex, and octal without doing the math by hand, use the Binary Converter. Paste in any number, pick your input base, and get all formats instantly.
FAQ
Q: What is the largest number you can store in 8 bits?
8 bits can represent values from 0 to 255 (unsigned), or −128 to 127 (signed, using two's complement). 11111111 in binary = 255 in decimal.
Q: Why does binary use only 0 and 1? Because computers are built from transistors that have two stable states: on (1) and off (0). It's the simplest reliable physical system for storing and processing information.
Q: How do I read a binary number like 10000000?
Only the most significant bit (position 7) is set: 1 × 2⁷ = 128. All other bits are 0. So 10000000 = 128.
Q: Is binary the same as boolean? Related but not the same. A boolean is a logical concept (true/false). A single binary bit can represent a boolean (1 = true, 0 = false), but binary numbers use multiple bits to represent integer values, not just true/false.
Q: What does "0b" mean in code?
It's a prefix indicating a binary literal. 0b1011 in Python, JavaScript, or Go means the number eleven written in binary. Similarly, 0x prefixes hex (0xFF = 255) and 0o prefixes octal (0o17 = 15).