Decimal to Binary Converter
Convert decimal numbers (base-10) to binary numbers (base-2)
Decimal to Binary Converter
Enter a decimal number to convert it to binary
Decimal to Binary Conversion
- Binary numbers use only digits 0 and 1
- Each position represents a power of 2
- Example: 10 in decimal = 1010 in binary (1×8 + 0×4 + 1×2 + 0×1)
Category Hub
Category Essentials
Number tools often serve connected search intents like conversion, formatting, and basic math references. These featured pages help users move between the main number tasks more naturally.
Related Tools
Daily Inspiration
The pen is mightier than the sword. - Edward Bulwer-Lytton
Decimal to Binary Converter: Convert Base 10 to Base 2
Convert decimal numbers to binary instantly with our free decimal to binary converter. Whether you're learning computer science, programming, or working with digital systems, this tool helps you convert base 10 numbers to base 2 (binary) format quickly and accurately.
Enter any decimal number (positive or negative, integer or fraction) to see the binary representation. Perfect for students, programmers, and anyone working with number systems.
What Is a Binary Number (Base 2)?
Binary is the base 2 number system, using only two digits: 0 and 1. It's the fundamental number system used by all digital computers and electronic systems.
Binary vs Decimal
Decimal (Base 10):
- Uses digits 0-9
- Each position represents a power of 10
- Example: 253 = (2 x 102) + (5 x 101) + (3 x 100)
Binary (Base 2):
- Uses only digits 0 and 1
- Each position represents a power of 2
- Example: 1101 = (1 x 23) + (1 x 22) + (0 x 21) + (1 x 20)
Why Binary Matters
Digital electronics:
- Computers process information in binary
- Electronic circuits have two states: on (1) or off (0)
- All data (numbers, text, images, sound) stored as binary
Fundamental to computing:
- Machine language uses binary
- Memory addresses in binary
- CPU operations work with binary
- Network protocols use binary
Binary Terminology
Bit: A single binary digit (0 or 1)
- Smallest unit of computer data
- Short for "binary digit"
LSB (Least Significant Bit):
- Rightmost bit
- Represents the smallest place value (20 = 1)
- Example: In 1101, LSB is 1
MSB (Most Significant Bit):
- Leftmost bit
- Represents the largest place value
- Example: In 1101, MSB is 1 (represents 23 = 8)
- In signed numbers, often indicates sign (0 = positive, 1 = negative)
Byte: 8 bits
- Common unit of computer storage
- Can represent 0-255 in unsigned binary
- Example: 11111111 = 255
How to Convert Decimal to Binary (Step-by-Step)
The standard method for converting decimal integers to binary is the successive division by 2 method.
Division by 2 Method (For Integers)
Algorithm:
- Divide the decimal number by 2
- Record the quotient and remainder
- Divide the quotient by 2
- Repeat until quotient is 0
- Read remainders from bottom to top
Example 1: Convert 13 to Binary
Step-by-step process:
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 13 / 2 | 6 | 1 |
| 2 | 6 / 2 | 3 | 0 |
| 3 | 3 / 2 | 1 | 1 |
| 4 | 1 / 2 | 0 | 1 |
Read remainders from bottom to top: 1101
Verification:
- 1101 in binary = (1 x 8) + (1 x 4) + (0 x 2) + (1 x 1)
- = 8 + 4 + 0 + 1 = 13
Example 2: Convert 25 to Binary
Step-by-step process:
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 25 / 2 | 12 | 1 |
| 2 | 12 / 2 | 6 | 0 |
| 3 | 6 / 2 | 3 | 0 |
| 4 | 3 / 2 | 1 | 1 |
| 5 | 1 / 2 | 0 | 1 |
Read remainders from bottom to top: 11001
Verification:
- 11001 = (1 x 16) + (1 x 8) + (0 x 4) + (0 x 2) + (1 x 1)
- = 16 + 8 + 0 + 0 + 1 = 25
Example 3: Convert 255 to Binary
Step-by-step process:
| Step | Division | Quotient | Remainder |
|---|---|---|---|
| 1 | 255 / 2 | 127 | 1 |
| 2 | 127 / 2 | 63 | 1 |
| 3 | 63 / 2 | 31 | 1 |
| 4 | 31 / 2 | 15 | 1 |
| 5 | 15 / 2 | 7 | 1 |
| 6 | 7 / 2 | 3 | 1 |
| 7 | 3 / 2 | 1 | 1 |
| 8 | 1 / 2 | 0 | 1 |
Result: 11111111 (eight 1s)
Note: 255 is the maximum value that fits in 8 bits (1 byte).
Why This Method Works
Each division by 2:
- Extracts one bit at a time
- Remainder is either 0 or 1 (the bit value)
- Quotient becomes the next number to process
Reading bottom to top:
- First remainder is LSB (least significant bit)
- Last remainder is MSB (most significant bit)
- This naturally places bits in correct positions
Alternative mental shortcut:
- Find largest power of 2 that fits
- Subtract it, mark that position as 1
- Repeat with remainder
- Fill remaining positions with 0
Decimal Fractions to Binary
Converting decimal fractions (numbers with decimal points) to binary uses a different method: multiply by 2.
Multiply by 2 Method (For Fractions)
Algorithm:
- Take the fractional part only
- Multiply by 2
- If result >= 1, record 1 and subtract 1; if result < 1, record 0
- Take new fractional part
- Repeat until fraction becomes 0 (or desired precision)
Example 1: Convert 0.25 to Binary
Step-by-step:
| Step | Fraction | x 2 | Integer Part | New Fraction |
|---|---|---|---|---|
| 1 | 0.25 | 0.5 | 0 | 0.5 |
| 2 | 0.5 | 1.0 | 1 | 0.0 |
Read integer parts top to bottom: 0.01
Verification:
- 0.01 in binary = (0 x 21) + (1 x 22)
- = 0 x 0.5 + 1 x 0.25 = 0.25
Example 2: Convert 0.75 to Binary
Step-by-step:
| Step | Fraction | x 2 | Integer Part | New Fraction |
|---|---|---|---|---|
| 1 | 0.75 | 1.5 | 1 | 0.5 |
| 2 | 0.5 | 1.0 | 1 | 0.0 |
Result: 0.11
Verification:
- 0.11 = (1 x 0.5) + (1 x 0.25) = 0.5 + 0.25 = 0.75
Example 3: Convert 0.625 to Binary
Step-by-step:
| Step | Fraction | x 2 | Integer Part | New Fraction |
|---|---|---|---|---|
| 1 | 0.625 | 1.25 | 1 | 0.25 |
| 2 | 0.25 | 0.5 | 0 | 0.5 |
| 3 | 0.5 | 1.0 | 1 | 0.0 |
Result: 0.101
Verification:
- 0.101 = (1 x 0.5) + (0 x 0.25) + (1 x 0.125)
- = 0.5 + 0 + 0.125 = 0.625
Example 4: Repeating Binary Fraction
Convert 0.1 (decimal) to binary:
| Step | Fraction | x 2 | Integer Part | New Fraction |
|---|---|---|---|---|
| 1 | 0.1 | 0.2 | 0 | 0.2 |
| 2 | 0.2 | 0.4 | 0 | 0.4 |
| 3 | 0.4 | 0.8 | 0 | 0.8 |
| 4 | 0.8 | 1.6 | 1 | 0.6 |
| 5 | 0.6 | 1.2 | 1 | 0.2 |
| 6 | 0.2 | 0.4 | 0 | 0.4 (cycle repeats) |
Result: 0.0001100110011... (repeating)
Key insight: Many decimal fractions that terminate (like 0.1) become infinitely repeating in binary. This is why computers can't represent 0.1 exactly.
Combining Integer and Fractional Parts
Example: Convert 13.25 to binary
Step 1: Convert integer part (13)
- 13 -> 1101 (as shown earlier)
Step 2: Convert fractional part (0.25)
- 0.25 -> 0.01 (as shown earlier)
Step 3: Combine with decimal point
- Result: 1101.01
Negative Numbers in Binary
There are several ways to represent negative numbers in binary. The most common method in computing is two's complement.
Sign-Magnitude Representation
Simple approach: Use leftmost bit for sign
- 0 = positive, 1 = negative
- Remaining bits represent magnitude
Example: 8-bit representation
- +5 = 00000101
- -5 = 10000101
Drawbacks:
- Two representations of zero (+0 and -0)
- Addition/subtraction require different logic
- Rarely used in modern computers
Two's Complement (Standard Method)
Two's complement is the standard way computers represent signed integers. It allows using the same circuitry for addition and subtraction.
How to convert negative decimal to two's complement:
Method 1 (Definition):
- Convert absolute value to binary
- Invert all bits (0->1, 1->0)
- Add 1 to the result
Method 2 (Alternative):
- Convert absolute value to binary
- Starting from right, keep bits same until first 1 (inclusive)
- Invert all remaining bits to the left
Example: Convert -5 to 8-bit Two's Complement
Method 1:
Step 1: Convert 5 to binary (8-bit)
- 5 = 00000101
Step 2: Invert all bits
- 11111010
Step 3: Add 1
- 11111010 + 1 = 11111011
Result: -5 in 8-bit two's complement = 11111011
Method 2 (same result):
- 5 = 00000101
- Keep rightmost bits up to and including first 1: ...101
- Invert remaining left bits: 11111011
Why Two's Complement Works
Key properties:
- MSB (leftmost bit) indicates sign (0 = positive, 1 = negative)
- Only one representation of zero
- Addition and subtraction use same circuit
- Range for n bits: -2^(n-1) to 2^(n-1) - 1
Example: 8-bit range
- Minimum: -128 (10000000)
- Maximum: +127 (01111111)
- Total: 256 values (28)
Two's Complement Conversion Table
| Decimal | 8-bit Two's Complement | Notes |
|---|---|---|
| 0 | 00000000 | Only one zero |
| 1 | 00000001 | Positive |
| 127 | 01111111 | Maximum positive |
| -1 | 11111111 | All 1s |
| -2 | 11111110 | |
| -127 | 10000001 | |
| -128 | 10000000 | Minimum (special case) |
Important Note on Bit Width
Two's complement requires a fixed bit width (8-bit, 16-bit, 32-bit, etc.). The same binary pattern means different things with different bit widths:
- 8-bit: 11111111 = -1
- 16-bit: 0000000011111111 = +255
- Without knowing bit width, interpretation is ambiguous
Binary Place Values and Bit Length
Understanding place values helps you read and construct binary numbers.
Binary Place Values (Powers of 2)
Each position in a binary number represents a power of 2:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Power of 2 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 |
| Decimal value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Example: Reading 10110101
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Binary | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| Value | 128 | 0 | 32 | 16 | 0 | 4 | 0 | 1 |
Total: 128 + 32 + 16 + 4 + 1 = 181
Fractional Place Values (Negative Powers of 2)
Positions right of the decimal point represent negative powers of 2:
| Position | -1 | -2 | -3 | -4 |
|---|---|---|---|---|
| Power of 2 | 21 | 22 | 23 | 24 |
| Decimal value | 0.5 | 0.25 | 0.125 | 0.0625 |
| Fraction | 1/2 | 1/4 | 1/8 | 1/16 |
Example: Reading 0.1011
- 0.1011 = (1 x 0.5) + (0 x 0.25) + (1 x 0.125) + (1 x 0.0625)
- = 0.5 + 0 + 0.125 + 0.0625 = 0.6875
How Many Bits Do I Need?
For unsigned (positive) integers:
The number of bits needed is: log2(n+1) (ceiling of log base 2)
Or more intuitively:
| Decimal Range | Bits Needed | Maximum Value |
|---|---|---|
| 0-1 | 1 bit | 1 (21 - 1) |
| 0-3 | 2 bits | 3 (22 - 1) |
| 0-7 | 3 bits | 7 (23 - 1) |
| 0-15 | 4 bits | 15 (24 - 1) |
| 0-31 | 5 bits | 31 (25 - 1) |
| 0-63 | 6 bits | 63 (26 - 1) |
| 0-127 | 7 bits | 127 (27 - 1) |
| 0-255 | 8 bits | 255 (28 - 1) |
| 0-65,535 | 16 bits | 65,535 (216 - 1) |
| 0-4,294,967,295 | 32 bits | ~4.3 billion |
For signed integers (two's complement):
| Decimal Range | Bits Needed | Range |
|---|---|---|
| -1 to 0 | 1 bit | -1 to 0 |
| -2 to 1 | 2 bits | -2 to 1 |
| -8 to 7 | 4 bits | -8 to 7 |
| -128 to 127 | 8 bits | -128 to 127 |
| -32,768 to 32,767 | 16 bits | +/-32K |
| -2,147,483,648 to 2,147,483,647 | 32 bits | +/-2.1B |
Rule of thumb: For a number n, you need at least the bit length of its binary representation.
Decimal to Binary Conversion Table
Quick reference for common decimal to binary conversions:
Powers of 2 (Common Values)
| Decimal | Binary | Note |
|---|---|---|
| 0 | 0 | Zero |
| 1 | 1 | 20 |
| 2 | 10 | 21 |
| 4 | 100 | 22 |
| 8 | 1000 | 23 |
| 16 | 10000 | 24 |
| 32 | 100000 | 25 |
| 64 | 1000000 | 26 |
| 128 | 10000000 | 27 |
| 256 | 100000000 | 28 |
| 512 | 1000000000 | 29 |
| 1024 | 10000000000 | 210 (1 KB) |
0-32 Conversion Table
| Decimal | Binary | Decimal | Binary |
|---|---|---|---|
| 0 | 0 | 17 | 10001 |
| 1 | 1 | 18 | 10010 |
| 2 | 10 | 19 | 10011 |
| 3 | 11 | 20 | 10100 |
| 4 | 100 | 21 | 10101 |
| 5 | 101 | 22 | 10110 |
| 6 | 110 | 23 | 10111 |
| 7 | 111 | 24 | 11000 |
| 8 | 1000 | 25 | 11001 |
| 9 | 1001 | 26 | 11010 |
| 10 | 1010 | 27 | 11011 |
| 11 | 1011 | 28 | 11100 |
| 12 | 1100 | 29 | 11101 |
| 13 | 1101 | 30 | 11110 |
| 14 | 1110 | 31 | 11111 |
| 15 | 1111 | 32 | 100000 |
| 16 | 10000 |
Common Byte Values
| Decimal | Binary | Hexadecimal |
|---|---|---|
| 127 | 01111111 | 7F |
| 128 | 10000000 | 80 |
| 255 | 11111111 | FF |
Common Use Cases
Understanding where binary conversion is used helps contextualize its importance.
Programming and Computer Science
Bit flags and masks:
- Store multiple boolean values in one integer
- Example: File permissions (read=4, write=2, execute=1)
- 7 in binary (111) = read + write + execute
Bitwise operations:
- AND, OR, XOR, NOT operations on binary
- Used for encryption, compression, graphics
- Example: 1010 AND 1100 = 1000
Low-level programming:
- Assembly language uses binary directly
- Device drivers manipulate bits
- Embedded systems work with registers
Networking and IP Addresses
IP addresses:
- IPv4 addresses are 32-bit binary numbers
- Example: 192.168.1.1 = 11000000.10101000.00000001.00000001
Subnet masks:
- Define network and host portions
- Example: 255.255.255.0 = 11111111.11111111.11111111.00000000
MAC addresses:
- 48-bit hardware addresses
- Represented in hexadecimal, stored in binary
Data Storage and Encoding
Character encoding:
- ASCII uses 7 or 8 bits per character
- Unicode uses variable-length binary
- 'A' = 65 decimal = 01000001 binary
Color representation:
- RGB colors use 8 bits per channel
- Example: Red (255, 0, 0) = 11111111, 00000000, 00000000
File sizes:
- Kilobyte (KB) = 1024 bytes (210)
- Megabyte (MB) = 1024 KB (220)
- Binary multiples vs. decimal multiples
Digital Electronics
Logic gates:
- AND, OR, NOT gates process binary signals
- Foundation of all digital circuits
- CPU operations built from logic gates
Memory addressing:
- RAM addresses in binary
- 16-bit address = 216 = 65,536 locations
Developer Examples
Programming languages provide built-in functions for decimal to binary conversion.
Python
Using bin() function:
# Convert decimal to binary
number = 13
binary = bin(number)
print(binary) # Output: 0b1101
# Remove '0b' prefix
binary_without_prefix = bin(number)[2:]
print(binary_without_prefix) # Output: 1101
# More examples
print(bin(25)) # 0b11001
print(bin(255)) # 0b11111111
print(bin(-5)) # -0b101 (negative sign, not two's complement)Custom padding to fixed width:
# Pad to 8 bits
number = 13
binary_8bit = format(number, '08b')
print(binary_8bit) # Output: 00001101
# Pad to 16 bits
binary_16bit = format(number, '016b')
print(binary_16bit) # Output: 0000000000001101Two's complement for negative numbers:
# Two's complement (8-bit)
def to_twos_complement(n, bits=8):
if n >= 0:
return format(n, f'0{bits}b')
else:
return format((1 << bits) + n, f'0{bits}b')
print(to_twos_complement(-5, 8)) # 11111011
print(to_twos_complement(5, 8)) # 00000101JavaScript
Using toString(2) method:
// Convert decimal to binary
let number = 13;
let binary = number.toString(2);
console.log(binary); // Output: 1101
// More examples
console.log((25).toString(2)); // 11001
console.log((255).toString(2)); // 11111111
console.log((-5).toString(2)); // -101 (keeps negative sign)Padding to fixed width:
// Pad to 8 bits
let number = 13;
let binary8bit = number.toString(2).padStart(8, '0');
console.log(binary8bit); // 00001101
// Pad to 16 bits
let binary16bit = number.toString(2).padStart(16, '0');
console.log(binary16bit); // 0000000000001101Two's complement workaround:
// Two's complement for negative (8-bit)
function toTwosComplement(n, bits = 8) {
if (n >= 0) {
return n.toString(2).padStart(bits, '0');
} else {
return ((1 << bits) + n).toString(2);
}
}
console.log(toTwosComplement(-5, 8)); // 11111011
console.log(toTwosComplement(5, 8)); // 00000101Important JavaScript note: The toString() method works with radix 2-36. For negative numbers, it uses a minus sign, not two's complement.
Safe integer limits:
// JavaScript safe integer range
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 (2^53 - 1)
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
// For larger numbers, use BigInt
let bigNumber = 9007199254740992n; // Note the 'n' suffix
console.log(bigNumber.toString(2)); // Works with BigIntOther Languages
C/C++:
// Using bitset (C++)
#include <bitset>
std::bitset<8> binary(13);
std::cout << binary; // 00001101Java:
// Integer.toBinaryString()
int number = 13;
String binary = Integer.toBinaryString(number);
System.out.println(binary); // 1101Why Some Decimals Can't Be Represented Exactly in Binary
This is one of the most common sources of confusion in computing.
The Fundamental Problem
Decimal fractions that terminate nicely (like 0.1, 0.2, 0.3) often become infinitely repeating in binary.
Why this happens:
- Decimal uses powers of 10 (10, 100, 1000...)
- Binary uses powers of 2 (2, 4, 8, 16...)
- Fractions like 1/10 can be expressed exactly in decimal (0.1)
- But 1/10 cannot be expressed exactly as a sum of powers of 2
Example: Why 0.1 Repeats in Binary
0.1 in binary = 0.00011001100110011... (repeating)
Attempting to represent it:
| Binary Fraction | Decimal Value | Remaining |
|---|---|---|
| 0.0 | 0 | 0.1 |
| 0.00 | 0 | 0.1 |
| 0.000 | 0 | 0.1 |
| 0.0001 | 0.0625 | 0.0375 |
| 0.00011 | 0.09375 | 0.00625 |
| 0.000110 | 0.09375 | 0.00625 |
| 0.0001100 | 0.09375 | 0.00625 |
| 0.00011001 | 0.09765625 | 0.00234375 |
The pattern 1100 repeats infinitely. You can never get exactly 0.1.
Which Decimal Fractions Convert Exactly?
Rule: A decimal fraction converts exactly to binary if and only if it can be expressed as:
n / 2^k where n and k are integers
Examples that convert exactly:
- 0.5 = 1/2 = 0.1 (binary)
- 0.25 = 1/4 = 0.01 (binary)
- 0.125 = 1/8 = 0.001 (binary)
- 0.625 = 5/8 = 0.101 (binary)
- 0.75 = 3/4 = 0.11 (binary)
Examples that don't convert exactly:
- 0.1 = 1/10 (10 = 2 x 5, has factor 5)
- 0.2 = 1/5 (has factor 5)
- 0.3 = 3/10 (has factor 5)
- 0.6 = 3/5 (has factor 5)
IEEE 754 Floating-Point Representation
Computers store decimal numbers using IEEE 754 standard:
32-bit (float): 1 sign bit + 8 exponent bits + 23 mantissa bits 64-bit (double): 1 sign bit + 11 exponent bits + 52 mantissa bits
This means:
- Limited precision (not infinite)
- Numbers are rounded to nearest representable value
- 0.1 is approximated, not exact
Consequences:
# Python example
0.1 + 0.2 # Returns 0.30000000000000004, not 0.3
# Why?
# 0.1 rounded to nearest binary
# 0.2 rounded to nearest binary
# Sum produces slight errorBest practices:
- Avoid equality tests on floating-point (use tolerance)
- Use decimal libraries for money/finance
- Understand rounding can accumulate
Troubleshooting Common Issues
"I entered a decimal fraction and got a long/repeating result"
Problem: Decimal fractions like 0.1 create long or infinite binary representations.
Why it happens:
- Not all decimal fractions have exact binary equivalents
- 0.1, 0.2, 0.3, etc. repeat infinitely in binary
- Converters may show truncated or rounded results
Example:
- Input: 0.1
- Exact binary: 0.0001100110011... (repeating)
- Truncated to 20 bits: 0.00011001100110011001
Solution:
- This is mathematically correct
- For exact representation, stick to fractions like 0.5, 0.25, 0.125
- Or use decimal-based number systems for finance
"My result is longer/shorter than expected"
Problem: Binary result has more or fewer digits than anticipated.
Causes:
No leading zeros:
- Binary doesn't show unnecessary leading zeros
- 5 = 101 (not 00000101)
- To see fixed-width, specify bit length
Different bit widths:
- 8-bit: 00001101
- 16-bit: 0000000000001101
- Without specified width, minimal representation shown
Solution:
- Understand that 1101 and 00001101 are the same value
- Specify bit width if you need fixed format
- Use padding in code (as shown in developer examples)
"Large number doesn't match JavaScript result"
Problem: Very large numbers produce unexpected results in JavaScript.
Why it happens:
- JavaScript uses IEEE 754 double-precision
- Safe integer range: -(253 - 1) to (253 - 1)
- Numbers beyond this lose precision
Example:
// Loses precision
let large = 9007199254740993;
console.log(large); // 9007199254740992 (wrong!)
// Binary conversion affected
console.log(large.toString(2)); // Incorrect binarySolution:
- Use BigInt for large integers
- Add 'n' suffix: 9007199254740993n
- BigInt supports toString(2)
Safe vs Unsafe:
- Safe: 9007199254740991 (253 - 1)
- Unsafe: 9007199254740992+ (precision lost)
"Negative number doesn't match two's complement calculator"
Problem: Your negative binary doesn't match another calculator's two's complement result.
Why it happens:
Different representations:
- Sign-magnitude: -1010 (minus sign + magnitude)
- Two's complement: 10110 (8-bit example)
- Different tools use different formats
Bit width matters:
- -5 in 8-bit: 11111011
- -5 in 16-bit: 1111111111111011
- Same value, different representation
Python/JavaScript behavior:
bin(-5)produces "-0b101" (sign-magnitude)- Not two's complement
- Need custom function for two's complement
Solution:
- Verify which format you need
- Specify bit width for two's complement
- Use appropriate conversion method
"Result has 0b prefix"
Problem: Binary result shows "0b" prefix.
Why it happens:
- Python's
bin()function adds "0b" prefix - Indicates binary literal in code
- Standard programming notation
Solution:
- Remove prefix:
bin(n)[2:] - Or keep it if using in code
- Just cosmetic difference
Prefix conventions:
- Binary: 0b (e.g., 0b1101)
- Hexadecimal: 0x (e.g., 0xD)
- Octal: 0o (e.g., 0o15)
Frequently Asked Questions
1. How do you convert a decimal number to binary?
To convert a decimal integer to binary, use the division by 2 method:
Step-by-step process:
- Divide the decimal number by 2
- Record the quotient and remainder (0 or 1)
- Divide the quotient by 2
- Repeat until quotient becomes 0
- Read all remainders from bottom to top
Example: Convert 13 to binary
- 13 / 2 = 6 remainder 1
- 6 / 2 = 3 remainder 0
- 3 / 2 = 1 remainder 1
- 1 / 2 = 0 remainder 1
Reading remainders bottom to top: 1101
Verification: 1101 = 8 + 4 + 0 + 1 = 13
Why this works: Each division extracts one binary digit (bit). The remainder tells you whether that power of 2 is present (1) or absent (0) in the binary representation.
2. What is the fastest way to convert decimal to binary by hand?
The fastest manual method depends on your comfort with powers of 2:
Method 1: Division by 2 (Most systematic)
- Best for accuracy
- Works for any size number
- As described in FAQ 1
Method 2: Powers of 2 subtraction (Fastest if you know powers)
Steps:
- Find the largest power of 2 that fits in your number
- Mark that bit position as 1
- Subtract that power from your number
- Repeat with the remainder
- Mark unused positions as 0
Example: Convert 45 to binary
Powers of 2: 32, 16, 8, 4, 2, 1
- 45 - 32 = 13 (mark position 5: 100000)
- 13 - 8 = 5 (mark position 3: 101000)
- 5 - 4 = 1 (mark position 2: 101100)
- 1 - 1 = 0 (mark position 0: 101101)
Result: 101101
Quick mental tricks:
- Memorize powers of 2 up to 1024
- Recognize patterns (255 = 11111111, all 1s)
- Use halving: each bit represents half the previous
Fastest for small numbers: Just memorize 0-15!
3. What do MSB and LSB mean?
MSB (Most Significant Bit):
- Leftmost bit in a binary number
- Represents the largest power of 2
- Has the greatest impact on the number's value
- In signed numbers, often indicates the sign
LSB (Least Significant Bit):
- Rightmost bit in a binary number
- Represents 20 (value of 1)
- Has the smallest impact on the number's value
- Determines if number is odd (1) or even (0)
Example: In binary number 10110101
1 0 1 1 0 1 0 1
MSB LSB
(128) (1)Why it matters:
MSB significance:
- Changing MSB changes value by +/-128 (in 8-bit)
- In two's complement, MSB = sign bit
- MSB-first transmission (big-endian)
LSB significance:
- Changing LSB changes value by +/-1
- Determines odd/even parity
- LSB-first transmission (little-endian)
Practical use:
- Bit shifting operations
- Data transmission order
- Priority in rounding
4. How do you convert a decimal fraction (like 0.25) to binary?
To convert the fractional part of a decimal to binary, use the multiply by 2 method:
Step-by-step process:
- Take only the fractional part (digits after decimal point)
- Multiply by 2
- If result >= 1, record 1 and subtract 1 from result; if < 1, record 0
- Take the new fractional part
- Repeat steps 2-4 until fraction becomes 0 (or desired precision)
- Read recorded digits from top to bottom
Example: Convert 0.25 to binary
- 0.25 x 2 = 0.5 -> record 0, fractional part is 0.5
- 0.5 x 2 = 1.0 -> record 1, fractional part is 0.0 (done)
Result: 0.01
Verification: 0.01 = 0 x (1/2) + 1 x (1/4) = 0.25
Another example: Convert 0.625 to binary
- 0.625 x 2 = 1.25 -> record 1, new fraction 0.25
- 0.25 x 2 = 0.5 -> record 0, new fraction 0.5
- 0.5 x 2 = 1.0 -> record 1, new fraction 0.0 (done)
Result: 0.101
Verification: 0.101 = (1/2) + (1/8) = 0.5 + 0.125 = 0.625
For mixed numbers (integer + fraction):
- Convert integer part using division by 2
- Convert fractional part using multiply by 2
- Combine with decimal point: integer.fraction
Example: 13.25 -> 1101.01
5. Why can't 0.1 be represented exactly in binary?
0.1 cannot be represented exactly in binary because it creates an infinitely repeating pattern.
The mathematical reason:
Binary fractions can only represent values that are sums of negative powers of 2:
- 1/2 = 0.5
- 1/4 = 0.25
- 1/8 = 0.125
- 1/16 = 0.0625
- etc.
0.1 = 1/10:
- 10 = 2 x 5
- Has factor of 5 (not a power of 2)
- Cannot be expressed as sum of powers of 2 exactly
What happens:
Converting 0.1 to binary:
- 0.1 x 2 = 0.2 -> 0
- 0.2 x 2 = 0.4 -> 0
- 0.4 x 2 = 0.8 -> 0
- 0.8 x 2 = 1.6 -> 1
- 0.6 x 2 = 1.2 -> 1
- 0.2 x 2 = 0.4 -> 0 (cycle repeats)
Result: 0.0001100110011... (pattern "1100" repeats forever)
Analogy: Just like 1/3 can't be represented exactly in decimal (0.333...), some fractions can't be represented exactly in binary.
Consequences in computing:
- Computers use finite precision (IEEE 754)
- 0.1 gets rounded to nearest representable value
- Small errors accumulate: 0.1 + 0.2 != 0.3 exactly
What converts exactly:
- Fractions like 0.5, 0.25, 0.125, 0.625 (powers of 2 denominators)
- NOT fractions like 0.1, 0.2, 0.3, 0.7 (have non-2 factors)
6. Does this tool support negative numbers? What format is used?
Yes, this tool supports negative numbers.
Format options depend on the implementation:
Option 1: Sign-magnitude (most common for general converters)
- Uses minus sign prefix
- Example: -13 -> -1101
- Simple and intuitive
- Not how computers store negatives internally
Option 2: Two's complement (computer science standard)
- Fixed-bit-width representation
- Example: -13 in 8-bit -> 11110011
- How computers actually store negative integers
- Requires specifying bit width
Most online converters (including typical implementations) use sign-magnitude for simplicity:
- Input: -25
- Output: -11001 (minus sign plus magnitude)
For two's complement output:
- Typically requires selecting bit width (8, 16, 32, 64)
- Some tools have a checkbox or dropdown
- More appropriate for low-level programming contexts
Check your specific tool to confirm which format it uses.
7. What is two's complement and when do I need it?
Two's complement is the standard method computers use to represent signed (positive and negative) integers in binary.
How it works:
- Uses a fixed number of bits (8, 16, 32, 64, etc.)
- MSB (leftmost bit) indicates sign: 0 = positive, 1 = negative
- Positive numbers: standard binary
- Negative numbers: invert all bits and add 1
Why computers use it:
- Only one representation of zero
- Addition and subtraction use the same circuit
- Extends naturally to all bit widths
- Efficient in hardware
When you need two's complement:
Low-level programming:
- Assembly language
- Bit manipulation
- Device drivers
- Embedded systems
Computer architecture:
- Understanding how CPUs work
- Memory representation
- Register operations
Networking:
- Some protocols use two's complement
- Binary data structures
You DON'T need it for:
- General decimal-to-binary conversions
- Learning binary number system
- Basic math in binary
- Most high-level programming
Example: -5 in 8-bit two's complement
Method:
- 5 in binary: 00000101
- Invert bits: 11111010
- Add 1: 11111011
Result: -5 = 11111011
Verification: 11111011 + 00000101 = 100000000 (overflow discarded) = 00000000 = 0
8. What does the 0b prefix mean in binary numbers?
The 0b prefix (or 0B) is a notation used in programming to indicate a binary literal.
Purpose: Tells the compiler/interpreter that the following digits are binary (base 2), not decimal.
Examples:
binary_num = 0b1101 # This is 13 in decimal
another = 0b11001 # This is 25 in decimalWithout the prefix: The number would be interpreted as decimal
wrong = 1101 # This is one thousand one hundred one (decimal), not 13!Language support:
Python:
print(0b1101) # Output: 13
print(bin(13)) # Output: 0b1101JavaScript (ES6+):
let binary = 0b1101; // 13
console.log(binary); // 13C/C++ (C++14+):
int binary = 0b1101; // 13Java (Java 7+):
int binary = 0b1101; // 13Other prefixes in programming:
- 0x or 0X: Hexadecimal (base 16)
- Example: 0x1A = 26 decimal
- 0o or 0O: Octal (base 8)
- Example: 0o17 = 15 decimal
- No prefix: Decimal (base 10)
- Example: 13 = 13 decimal
When you see 0b: The number is written in binary notation for clarity in code.
Removing the prefix: Many tools show 0b in output. To get just the binary digits, remove the first two characters.
9. How many bits do I need to represent my number?
The number of bits needed depends on whether your number is positive only or includes negative numbers.
For positive integers (unsigned):
Formula: Bits needed = log2(n + 1)
Or more simply: Find the length of the binary representation
Quick reference:
| Number Range | Bits Needed |
|---|---|
| 0 to 1 | 1 bit |
| 0 to 3 | 2 bits |
| 0 to 7 | 3 bits |
| 0 to 15 | 4 bits |
| 0 to 31 | 5 bits |
| 0 to 63 | 6 bits |
| 0 to 127 | 7 bits |
| 0 to 255 | 8 bits |
| 0 to 65,535 | 16 bits |
| 0 to 4,294,967,295 | 32 bits |
Rule: For number n, count the binary digits
- 13 -> 1101 -> 4 bits minimum
- 255 -> 11111111 -> 8 bits
- 1000 -> 1111101000 -> 10 bits
For signed integers (with negatives):
You need one extra bit for the sign.
Common sizes:
- 8-bit signed: -128 to +127
- 16-bit signed: -32,768 to +32,767
- 32-bit signed: -2,147,483,648 to +2,147,483,647
- 64-bit signed: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Examples:
- Number 13: needs 4 bits unsigned, or 8 bits signed (typical)
- Number 200: needs 8 bits unsigned, or 16 bits signed (typical)
- Number -13: needs at least 8 bits (using two's complement)
Practical choices: Use standard sizes (8, 16, 32, 64 bits) even if you need fewer.
10. How do I convert binary back to decimal?
To convert binary to decimal, multiply each bit by its place value (power of 2) and sum the results.
Step-by-step method:
- Write the binary number
- Number positions from right to left, starting at 0
- For each bit that is 1, calculate 2^(position)
- Add all these values
Example: Convert 1101 to decimal
Binary: 1 1 0 1
Position: 3 2 1 0
Value: 8 4 2 1Calculation:
- Position 3: 1 x 23 = 1 x 8 = 8
- Position 2: 1 x 22 = 1 x 4 = 4
- Position 1: 0 x 21 = 0 x 2 = 0
- Position 0: 1 x 20 = 1 x 1 = 1
Sum: 8 + 4 + 0 + 1 = 13
Quick method: Use place values
128 64 32 16 8 4 2 1
1 0 1 1 0 1 0 1
128 + 32 + 16 + 4 + 1 = 181For binary fractions:
Use negative powers of 2 for digits right of decimal point.
Example: 0.101 to decimal
Position: -1 -2 -3
Power: 21 22 23
Value: 0.5 0.25 0.125
Binary: 1 0 1Calculation: (1 x 0.5) + (0 x 0.25) + (1 x 0.125) = 0.5 + 0 + 0.125 = 0.625
For mixed numbers:
- 1101.101 = 13.625
- Convert integer part: 1101 = 13
- Convert fractional part: 0.101 = 0.625
- Combine: 13 + 0.625 = 13.625
Use our Binary to Decimal Converter for instant results!
12. Why does JavaScript's result differ for large numbers?
JavaScript loses precision with integers beyond a certain size due to its number representation.
The problem:
JavaScript uses IEEE 754 double-precision floating-point for all numbers (until BigInt was introduced).
Safe integer range:
- Minimum: -(253 - 1) = -9,007,199,254,740,991
- Maximum: +(253 - 1) = +9,007,199,254,740,991
Beyond this range, integers lose precision:
// Precision lost
console.log(9007199254740992); // 9007199254740992
console.log(9007199254740993); // 9007199254740992 (wrong!)
console.log(9007199254740993 + 1); // 9007199254740994 (skipped!)
// Binary conversion affected
console.log((9007199254740993).toString(2));
// Result will be wrong due to precision lossWhy this happens:
IEEE 754 double precision:
- 1 sign bit
- 11 exponent bits
- 52 mantissa bits
- Can only represent 253 distinct integers exactly
Above 253: Numbers are spaced further apart
- Can represent 253, but not 253 + 1
- Can represent 253 + 2, but not 253 + 3
- Gaps increase as numbers get larger
Solution: Use BigInt:
// BigInt for large integers (ES2020+)
let large = 9007199254740993n; // Note the 'n' suffix
console.log(large); // 9007199254740993n (correct!)
// Binary conversion works
console.log(large.toString(2));
// Correct binary representation
// Arithmetic works
console.log(large + 1n); // 9007199254740994n (correct!)Checking for safe integers:
Number.isSafeInteger(9007199254740991); // true
Number.isSafeInteger(9007199254740992); // false
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.MIN_SAFE_INTEGER; // -9007199254740991Important notes:
- BigInt cannot mix with regular numbers in operations
- BigInt doesn't work with Math methods
- BigInt has full precision for any size integer
- Use BigInt when working with large IDs, timestamps, or precise calculations
