Binary, decimal and hex converter
Enter a number and its base, get it in every base instantly.
How do I convert decimal to binary?
Divide by 2 again and again and write down the remainders, last one first. 13 ÷ 2 = 6 r 1, 6 ÷ 2 = 3 r 0, 3 ÷ 2 = 1 r 1, 1 ÷ 2 = 0 r 1 — reading up, 13 is 1101 in binary.
How do I convert binary to decimal?
Add up the powers of 2 where there is a 1, counting from 0 on the right. 1101 = 8 + 4 + 0 + 1 = 13; 1111 1111 = 255.
How does hexadecimal work?
Base 16 uses 0–9 and A–F for 10–15. Each hex digit is exactly four binary digits, so 255 = 1111 1111 = FF, and web colors like #FF6A2B are three hex bytes.
What is octal?
Base 8, digits 0–7. Each octal digit is three binary digits: 255 is 377 in octal. It shows up in Unix file permissions like 755.
How large a number can it convert?
Any length — the converter uses exact big-integer arithmetic, so 18,446,744,073,709,551,615 (the largest 64-bit unsigned number, FFFF FFFF FFFF FFFF) converts without rounding.
What is the largest number that fits in 8, 16 and 32 bits?
An unsigned n-bit number runs from 0 to 2^n − 1: 255 for 8 bits, 65,535 for 16 bits and 4,294,967,295 for 32 bits. For 64 bits the top value is 18,446,744,073,709,551,615.
How are negative numbers written in binary?
Computers usually store them in two's complement, where an 8-bit signed number runs from −128 to 127. This converter shows a negative number with a minus sign in front of the ordinary binary form, for example −5 as −101, which is not a two's complement bit pattern.
How do I add two binary numbers?
Work from the right as in decimal addition: 0 + 0 = 0, 0 + 1 = 1, and 1 + 1 = 10, which means write 0 and carry 1. For example 1011 + 110 = 10001, which is 11 + 6 = 17.
How the conversion works
A binary number is a sum of powers of 2, one for each place, counted from 0 at the right: 1, 2, 4, 8, 16 and so on. A digit of 1 means that power is included, and a 0 means it is left out. To go from decimal to binary, find which powers of 2 add up to the number, or divide by 2 repeatedly and read the remainders backward. Octal and hexadecimal are shorthand for binary: three bits make one octal digit, and four bits make one hex digit. The converter works on whole numbers and uses exact integer arithmetic.
Worked example
Convert 202 to binary. The powers of 2 that make 202 are 128 + 64 + 8 + 2, so the binary number is 11001010. Written in groups of four bits, 1100 1010, each group becomes one hex digit, which gives CA in hexadecimal. Grouped in threes from the right, the same bits give 312 in octal. The reverse check: CA in hex is 12 × 16 + 10 = 202, because the letters stand for 10 to 15.
Common mistakes
- Reading the bits left to right as powers. The rightmost bit is the 1s place, so 1101 is 8 + 4 + 0 + 1 = 13 and not 1 + 2 + 0 + 8.
- Grouping bits from the left. Groups of four start from the right edge, with zeros added at the front if needed. 1011010 is 0101 1010, which is 5A in hex, and not 1011 010.
- Treating the output as a fixed-width register. The result is the shortest binary form, with no leading zeros. A byte is normally written with all 8 bits, so 5 shows here as 101 but is 00000101 in an 8-bit field.
Sources and related calculators
Method: positional notation and radix conversion, as described in Donald Knuth, The Art of Computer Programming, volume 2, section 4.1. Related: number base converter, hex calculator and color converter.