Decimal to Binary — How to Convert Numbers Between Bases
Decimal to binary, binary to decimal, hex to binary — number base conversion explained step by step. Includes conversion tables, formulas, and code examples.
Decimal is the number system humans use. Binary is the number system computers use. Converting between them is a core skill for understanding how computers represent data — whether you’re debugging a bitmask, reading memory dumps, or learning computer architecture.
Use the Number Base Converter on this site to convert between binary (base-2), decimal (base-10), hexadecimal (base-16), and octal (base-8) instantly.
Why computers use binary
Transistors, the fundamental building block of modern CPUs, are switching devices: they’re either on or off. That maps naturally to 1 and 0. Building circuits that represent 10 distinct values (for decimal) would require far more complex hardware than circuits that represent 2 values. Binary is the natural language of digital electronics.
From binary, everything else is derived:
- Octal (base-8): 3 binary bits grouped together — used in Unix file permissions (
chmod 755) - Hexadecimal (base-16): 4 binary bits grouped together — used for memory addresses, color codes, error codes
- Decimal: the human-friendly representation we convert to for display
Decimal to binary conversion
Method 1: Repeated division by 2
Divide the decimal number by 2 repeatedly. Record each remainder (0 or 1). The binary number is the remainders read in reverse order.
Example: convert 43 to binary
| Division | Quotient | Remainder |
|---|---|---|
| 43 ÷ 2 | 21 | 1 |
| 21 ÷ 2 | 10 | 1 |
| 10 ÷ 2 | 5 | 0 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Read remainders bottom to top: 101011
Verify: 1×2⁵ + 0×2⁴ + 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 32 + 0 + 8 + 0 + 2 + 1 = 43 ✓
Method 2: Subtract largest power of 2
Find the largest power of 2 that fits in your number. Subtract it (write a 1). Repeat for the remainder. Write 0 for each power of 2 that doesn’t fit.
Example: convert 100 to binary
Powers of 2 up to 100: 64, 32, 16, 8, 4, 2, 1
- 64 ≤ 100: write 1, remainder = 100 - 64 = 36
- 32 ≤ 36: write 1, remainder = 36 - 32 = 4
- 16 > 4: write 0
- 8 > 4: write 0
- 4 ≤ 4: write 1, remainder = 4 - 4 = 0
- 2 > 0: write 0
- 1 > 0: write 0
Binary: 1100100
Verify: 64 + 32 + 4 = 100 ✓
Binary to decimal conversion
Sum each bit multiplied by 2 raised to its position (starting at 0 from the right).
Example: convert 10110101 to decimal
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 1 |
| Value | 128 | 0 | 32 | 16 | 0 | 4 | 0 | 1 |
128 + 32 + 16 + 4 + 1 = 181
Quick binary-to-decimal reference
| Binary | Decimal | Hex |
|---|---|---|
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | 10 | A |
| 1011 | 11 | B |
| 1100 | 12 | C |
| 1101 | 13 | D |
| 1110 | 14 | E |
| 1111 | 15 | F |
Memorize this table and you can convert any hex digit to 4 bits instantly.
Decimal to hexadecimal
Method: divide by 16 (like base-2 but divide by 16)
Example: convert 255 to hex
| Division | Quotient | Remainder |
|---|---|---|
| 255 ÷ 16 | 15 | 15 → F |
| 15 ÷ 16 | 0 | 15 → F |
Read bottom to top: FF
Verify: 15×16¹ + 15×16⁰ = 240 + 15 = 255 ✓
Common decimal to hex conversions
| Decimal | Hex | Binary |
|---|---|---|
| 0 | 0x00 | 00000000 |
| 127 | 0x7F | 01111111 |
| 128 | 0x80 | 10000000 |
| 255 | 0xFF | 11111111 |
| 256 | 0x100 | 100000000 |
| 65535 | 0xFFFF | 1111111111111111 |
| 65536 | 0x10000 | 10000000000000000 |
These boundary values are significant in computing: 127 is the max signed 8-bit integer, 128 is where the sign bit flips, 255 is max unsigned 8-bit, 65535 is max unsigned 16-bit.
Hexadecimal to binary (and back)
This is the most useful conversion for programmers because it’s instantaneous once you know the 4-bit groups.
Hex to binary: replace each hex digit with its 4-bit binary equivalent.
0xFF = F F
= 1111 1111
= 11111111
0xAD = A D
= 1010 1101
= 10101101
0xDEADBEEF = D E A D B E E F
= 1101 1110 1010 1101 1011 1110 1110 1111
Binary to hex: group bits into groups of 4 from the right, convert each group.
1011011010 = 10 1101 1010
= 2 D A
= 0x2DA
Pad the leftmost group with zeros if needed: 10 becomes 0010.
Octal conversion
Octal (base-8) groups binary into groups of 3 bits. Each octal digit represents 3 bits.
| Octal | Binary |
|---|---|
| 0 | 000 |
| 1 | 001 |
| 2 | 010 |
| 3 | 011 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
Octal relevance today: Unix file permissions
chmod 755 means:
7= 111 binary = read(4) + write(2) + execute(1) for owner5= 101 binary = read(4) + execute(1) for group5= 101 binary = read(4) + execute(1) for others
chmod 644:
6= 110 = read + write for owner4= 100 = read only for group4= 100 = read only for others
Understanding octal-binary mapping makes Unix permissions immediately readable.
Two’s complement: negative binary numbers
Positive integers have a straightforward binary representation. Negative numbers in computer hardware use two’s complement notation.
To negate a binary number in two’s complement:
- Invert all bits (one’s complement)
- Add 1
Example: represent -5 in 8-bit two’s complement
- Start with 5:
00000101 - Invert all bits:
11111010 - Add 1:
11111011
-5 in 8-bit two’s complement = 11111011
Verify: 11111011 as unsigned = 128+64+32+16+8+0+2+1 = 251 = 256 - 5 ✓
Two’s complement is why 0xFF in a signed 8-bit integer is -1, not 255. This is a common source of bugs when mixing signed and unsigned integer types.
uint8_t a = 0xFF; // 255 (unsigned)
int8_t b = 0xFF; // -1 (signed two's complement)
// In C, signed overflow is undefined behavior:
int8_t c = 127;
c++; // Undefined behavior — don't do this
Number base conversion in programming
Python
# Decimal to other bases:
bin(255) # '0b11111111' — prefix 0b
oct(255) # '0o377' — prefix 0o
hex(255) # '0xff' — prefix 0x
# Strip prefix:
bin(255)[2:] # '11111111'
format(255, 'b') # '11111111'
format(255, '08b') # '11111111' (zero-padded to 8 bits)
format(255, 'x') # 'ff'
format(255, 'X') # 'FF' (uppercase)
format(255, 'o') # '377'
# Convert from other bases to decimal:
int('11111111', 2) # 255 (binary → decimal)
int('ff', 16) # 255 (hex → decimal)
int('377', 8) # 255 (octal → decimal)
JavaScript
// Decimal to other bases:
(255).toString(2) // '11111111'
(255).toString(8) // '377'
(255).toString(16) // 'ff'
// Other bases to decimal:
parseInt('11111111', 2) // 255
parseInt('ff', 16) // 255
parseInt('377', 8) // 255
// Zero-padded hex:
(255).toString(16).padStart(4, '0') // '00ff'
C
#include <stdio.h>
int main() {
int n = 255;
printf("%d\n", n); // Decimal: 255
printf("%o\n", n); // Octal: 377
printf("%x\n", n); // Hex: ff
printf("%X\n", n); // Hex: FF
// Binary requires manual conversion:
for (int i = 7; i >= 0; i--)
printf("%d", (n >> i) & 1);
printf("\n"); // Binary: 11111111
return 0;
}
Bit manipulation fundamentals
Once you can convert between bases, bit manipulation becomes readable:
| Operation | Symbol | Example | Result |
|---|---|---|---|
| AND | & | 0b1010 & 0b1100 | 0b1000 (8) |
| OR | | | 0b1010 | 0b1100 | 0b1110 (14) |
| XOR | ^ | 0b1010 ^ 0b1100 | 0b0110 (6) |
| NOT | ~ | ~0b1010 (8-bit) | 0b11110101 (245 unsigned, -11 signed) |
| Left shift | << | 1 << 3 | 8 (multiply by 2³) |
| Right shift | >> | 16 >> 2 | 4 (divide by 2²) |
Common patterns:
- Check if bit n is set:
(value >> n) & 1 - Set bit n:
value | (1 << n) - Clear bit n:
value & ~(1 << n) - Toggle bit n:
value ^ (1 << n)
Related tools
- Number Base Converter — convert between binary, decimal, hex, and octal
- Hash Generator — compute checksums of strings
- Base64 Encoder/Decoder — encode binary data as text
Related posts
- Binary Arithmetic — Addition, Subtraction, and Two's Complement — Learn how computers perform binary arithmetic: binary addition with carry, two's…
- Binary to Decimal — Convert Binary Numbers the Right Way — Binary to decimal conversion is foundational to understanding how computers stor…
- Binary to Text: How Binary Numbers Represent Characters — Binary to text conversion isn't magic — it's a lookup table. ASCII, Unicode, UTF…
Related tool
Convert between binary, octal, decimal, hexadecimal, and text (UTF-8). Handles arbitrary lengths. Per-byte and per-character views.
Written by Mian Ali Khalid. Part of the Encoding & Crypto pillar.