Decimal to Binary Converter
Decimal to binary converter: divide by 2 again and again and read the remainders from the last to the first, so 181 becomes 10110101. Whole numbers of any size and lists are converted exactly, with grouping in fours and a 0b prefix as options, and the division table is shown.
| Divide | Quotient | Remainder |
|---|---|---|
| 181 | 90 | 1 |
| 90 | 45 | 0 |
| 45 | 22 | 1 |
| 22 | 11 | 0 |
| 11 | 5 | 1 |
| 5 | 2 | 1 |
| 2 | 1 | 0 |
| 1 | 0 | 1 |
Decimal to binary: divide by 2 and keep the remainders
The standard way to convert a decimal whole number to binary is repeated division by 2. At each step you divide by 2, write down the remainder, which can only be 0 or 1, and carry on with the quotient. When the quotient reaches 0 you stop. The remainders are the binary digits, and the first remainder is the lowest bit, so they have to be read from the last one to the first. The converter above shows this table for a single value under the result.
The idea behind it is simple. A remainder of 1 after dividing by 2 means the number is odd, so the lowest bit is 1. Removing that bit and dividing by 2 gives the number that the remaining bits represent, and the same question applies again to that number. Repeating it peels the bits off one at a time from the right.
A worked example with 181
Start with 181. It is odd, so the remainder is 1 and the quotient is 90. Then 90 is even, remainder 0, quotient 45; 45 is odd, remainder 1, quotient 22; and so on until the quotient is 1 and the final division gives remainder 1 and quotient 0. Written out, the sequence is in the table, and the remainders read from the bottom up spell 10110101.
| Divide | Quotient | Remainder |
|---|---|---|
| 181 ÷ 2 | 90 | 1 |
| 90 ÷ 2 | 45 | 0 |
| 45 ÷ 2 | 22 | 1 |
| 22 ÷ 2 | 11 | 0 |
| 11 ÷ 2 | 5 | 1 |
| 5 ÷ 2 | 2 | 1 |
| 2 ÷ 2 | 1 | 0 |
| 1 ÷ 2 | 0 | 1 |
Count the steps: there are eight divisions, and the number has eight bits. In general the number of bits is the number of divisions needed to reach zero. You can confirm the result by converting back with the binary to decimal page: 128 + 32 + 16 + 4 + 1 gives 181.
The subtraction method
Many people prefer to work from the top. Write the powers of two from the largest one that is not bigger than the number, and for each power ask whether it fits into what is left. If it does, write 1 and subtract it. If not, write 0 and go on. The bits come out from left to right, which is the order in which you write them, so there is nothing to reverse.
| Power of two | Fits in what is left? | Bit | Left over |
|---|---|---|---|
| 128 | yes | 1 | 53 |
| 64 | no | 0 | 53 |
| 32 | yes | 1 | 21 |
| 16 | yes | 1 | 5 |
| 8 | no | 0 | 5 |
| 4 | yes | 1 | 1 |
| 2 | no | 0 | 1 |
| 1 | yes | 1 | 0 |
Both methods find the same unique answer. Division is easier to carry out mechanically and is the one programs use. Subtraction is easier to do in your head when the number is close to a power of two, since you will know the powers by heart: 181 is 128 plus 53, and 53 is 32 plus 21, and so on. For numbers that are exactly one less than a power of two, such as 255, the answer is a string of ones, with no arithmetic needed.
Padding to a fixed width
The result of the division method has no leading zeros, so 5 comes out as 101. Computer data usually has a fixed width. A byte is 8 bits, and the number 5 stored in one byte is 00000101; in a 16-bit field it is 0000000000000101. The extra zeros change nothing about the value. The converter has an option to group the output in fours, which makes long strings easier to read and prepares them for conversion to hex.
When you must fit a value in a given width, the largest unsigned number is 2 to the power of the width, minus one. 8 bits hold up to 255, 16 bits up to 65,535 and 32 bits up to 4,294,967,295. A bigger number needs more bits, and it is better to see that on paper than to find it as a truncated value later.
Decimal numbers worth knowing in binary
You meet the same numbers over and over in networking, storage and programming. Seeing their binary form together helps you recognise the pattern behind them: powers of two have a single 1, the number one below a power of two is all ones, and an even number ends in 0.
| Decimal | Binary | Hex |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 10 | 2 |
| 3 | 11 | 3 |
| 5 | 101 | 5 |
| 8 | 1000 | 8 |
| 10 | 1010 | A |
| 16 | 10000 | 10 |
| 32 | 100000 | 20 |
| 64 | 1000000 | 40 |
| 100 | 1100100 | 64 |
| 127 | 1111111 | 7F |
| 128 | 10000000 | 80 |
| 200 | 11001000 | C8 |
| 255 | 11111111 | FF |
| 256 | 100000000 | 100 |
| 1,000 | 1111101000 | 3E8 |
| 1,024 | 10000000000 | 400 |
| 65,535 | 1111111111111111 | FFFF |
Negative decimal numbers
The conversion above is for zero and positive whole numbers. If you type a minus sign the converter keeps it and converts the magnitude, so -5 becomes -101. That is the sign and magnitude notation used in mathematics. It is not how a computer stores a negative integer, which uses two's complement: in 8 bits, −5 is 11111011. The stored form depends on the width, since the same value is 11111011 in 8 bits and 1111111111111011 in 16 bits.
To get the stored pattern for a negative number, with the flip-and-add-one steps shown, use the two's complement calculator. The decimal to hex converter also shows the two's complement form of a negative number in 8, 16, 32 or 64 bits.
Decimal fractions
A fraction is converted with the opposite operation: multiply the fractional part by 2, write the whole part of the product as the next bit, and continue with what is left until it is zero. For 0.625 the products are 1.25, 0.5 and 1.0, so the bits are 1, 0, 1 and the binary fraction is 0.101, which is 1/2 + 1/8. This tool converts whole numbers only, so a decimal point in the input is reported as an error.
Most decimal fractions never terminate. 0.1 produces the endless pattern 0.0001100110011…, which is why computers can only approximate it, and why 0.1 + 0.2 does not equal 0.3 exactly in floating point. The float to hex converter shows the nearest stored value and the size of the error for any decimal number.
Large numbers and lists
The converter uses arbitrary-precision integers, so a 30-digit decimal number converts without losing a digit. To convert many values, put one number on each line, or separate them with spaces or commas, and the output has one binary number per line in the same order. Characters that are not decimal digits are named with their position. Underscores inside a number are ignored, but a comma starts a new value, so 1,000 is read as the two numbers 1 and 000.
Character codes in binary
A frequent reason to convert decimal to binary is a character code. The ASCII code of A is 65, which is 01000001 in binary, and a is 97, which is 01100001. The only difference is the bit worth 32, which is why adding 32 to a capital letter code gives the lower case letter. If you have a decimal code and want the character, read the ASCII table, and for a whole message use the binary to text converter, which turns text into bytes and shows every byte in binary.
Decimal to binary for addresses and permissions
Two everyday uses are IP addresses and file permissions. An IPv4 address such as 192.168.1.1 is four decimal numbers, each one byte, and converting each to eight bits gives 11000000 10101000 00000001 00000001. A subnet mask like 255.255.255.0 is three bytes of ones and one of zeros, which is how the prefix length /24 is counted: the number of leading ones. Converting a mask byte that is not 255 or 0, say 192, shows the ones that belong to the network part, 11000000, two ones and six zeros.
For Unix file permissions, each digit of a mode such as 755 is converted separately to three bits: 7 is 111, 5 is 101. The three bits mean read, write and execute in that order, so 755 is read, write and execute for the owner and read and execute for everyone else. With the table of numbers above and the method on this page, any permission digit is a conversion you can do in your head.
Try it yourself
Convert these numbers with either method before checking them in the converter, then compare with the table. They cover a small number, a number just above a power of two, a power of two, a byte and a number that needs more than one byte.
| Decimal | Binary | Bits |
|---|---|---|
| 6 | 110 | 3 |
| 19 | 10011 | 5 |
| 37 | 100101 | 6 |
| 64 | 1000000 | 7 |
| 100 | 1100100 | 7 |
| 255 | 11111111 | 8 |
| 300 | 100101100 | 9 |
| 1000 | 1111101000 | 10 |
Reading the converter's output
With a single number the output box holds the binary digits, and the table below it lists every division. The status line gives the number of digits, which is also the number of bits the number needs. The Groups of four option puts a space after every four digits, counting from the right, and pads the first group with zeros, so 181 is written 1011 0101. The 0b prefix option adds the notation used in source code, 0b10110101, and removes the spaces so that the result can be pasted into a program as a literal.
When the input is a list, the output is a list in the same order, and a line that cannot be read is replaced by an empty line while the others are still converted. A message under the boxes says which line was the problem and why. Use the Copy button to take the whole result at once, and the Clear button to start again with an empty input.
Why computers use binary
A circuit that has to distinguish only two states, on and off, is easier to build reliably than one that distinguishes ten voltage levels, because small errors in the signal do not change which of the two states it is. Everything else follows from that. Arithmetic on binary numbers uses simple rules, with 1 + 1 = 10 as the only carry case, and any number can be stored as a row of switches. Binary is the form that hardware works with, and hex and decimal are conveniences for people.
Decimal to binary in code
The conversion is built into every language. These snippets were run and print 10110101 for 181.
(181).toString(2); // "10110101"
(181).toString(2).padStart(16, "0"); // "0000000010110101"
BigInt("123456789012345678901234567890").toString(2); // exactn=181; s=""
while [ $n -gt 0 ]; do s="$((n % 2))$s"; n=$((n / 2)); done
echo $s
# 10110101[Convert]::ToString(181, 2)
# 10110101The Bash loop is the division method written out. Bash has no built-in to print a number in base 2, which is why the loop is needed. Note that PowerShell's ToString(value, 2) writes negative numbers in two's complement at 32 or 64 bits, so for negative inputs the output has many leading ones.
Mistakes when dividing by two
Reading the remainders from the top down is the usual error: the first remainder is the lowest bit and must be written last. Stopping when the quotient is 1 instead of 0 drops the leading 1. Dividing a quotient by 2 and rounding it instead of using the integer part gives wrong bits, since 45 ÷ 2 is 22 remainder 1, and not 23. Finally, check the length: a number with n bits is at least 2ⁿ⁻¹, so a result with 7 digits for 181 is too short and one with 9 digits is too long.
For a full check, convert the answer back with the binary to decimal page, or look at the same number in several bases at once with the hex converter.
Questions about decimal to binary
How do you convert decimal to binary?
Divide the number by 2 and note the remainder, which is 0 or 1. Divide the quotient by 2 again, and repeat until the quotient is 0. The remainders, read from the last to the first, are the binary digits. For 13 the remainders are 1, 0, 1, 1 and the binary number is 1101.
What is 10 in binary?
1010. 10 ÷ 2 = 5 remainder 0, 5 ÷ 2 = 2 remainder 1, 2 ÷ 2 = 1 remainder 0 and 1 ÷ 2 = 0 remainder 1. Read from the bottom up, the remainders are 1, 0, 1, 0.
How do I write a number in 8 bits?
Convert it as usual and add zeros on the left until there are eight digits. 5 is 101, so in 8 bits it is 00000101. A number above 255 does not fit in 8 bits as an unsigned value.
How are negative decimal numbers written in binary?
Computers store them in two's complement, which depends on a chosen width such as 8 or 32 bits. The converter writes -5 as -101, a sign in front of the magnitude. For the stored form, 11111011 in 8 bits, use the two's complement calculator.
Is there a limit to the size of the decimal number?
No practical one. The converter works with arbitrary-precision integers and keeps every digit. Numbers beyond 2 to the power 53 lose digits in languages that use floating point for all numbers, which this tool does not.
