Number Theory & Bases

Decimal to Binary Converter

To convert decimal to binary, divide the number by 2 again and again and write down each remainder; the remainders read from the bottom up are the binary digits.

Try

Whole numbers, negatives and decimals such as 0.1. Spaces and thousands commas are ignored.

Pads with leading zeros to a full byte or word. Negative numbers become two’s complement at this width.

Result

13 in binary: 1101

13 in binary

110121101_{2}

8 bits (one byte)
0000 1101
Hexadecimal
D
Octal
15
Bits neededEvery number from 8 to 15 needs 4 bits
4

13 in decimal is 1101 in binary.

Dividing 13 by 2: read the remainders from the bottom up
DivideQuotientRemainder (bit)Bit position
13 ÷ 2612⁰
6 ÷ 2302¹
3 ÷ 2112²
1 ÷ 2012³
Step-by-step working (4 steps)
  1. Divide by 2 repeatedly

    Keep dividing by 2 and write down each remainder: 13 ÷ 2 = 6 r 1; 6 ÷ 2 = 3 r 0; 3 ÷ 2 = 1 r 1; 1 ÷ 2 = 0 r 1. Stop when the quotient reaches 0.

  2. Read the remainders from the bottom up

    The first remainder is the rightmost bit (the 1s place) and the last is the leftmost: 13 = 1101₂.

  3. Check with place values

    The 1 bits sit at 2³, 2², 2⁰: 8 + 4 + 1 = 13.

  4. Result

    1310=1101213_{10} = 1101_{2}

So 13 becomes 1101. Type any number to see every division, pad the answer to 8, 16, 32 or 64 bits, and convert negative numbers to two’s complement and decimals such as 0.625 to binary fractions.

By Hira Noor, mathematicianLast reviewed Free, no sign-up, runs in your browserReport a problem

How to use this calculator

  1. Type the decimal number, for example 13 or 156.625. A minus sign in front, such as −5, gives a negative number.
  2. The binary answer appears as you type, with a table of every division by 2, its quotient and its remainder.
  3. To get a fixed number of bits, set Bit width to 8, 16, 32 or 64. The answer is padded with leading zeros: 13 becomes 0000 1101 as one byte.
  4. For a negative number, pick a bit width to get its two’s complement pattern: −5 in 8 bits is 1111 1011.
  5. Going the other way? The binary to decimal converter adds up each bit’s place value, and the number base converter handles hex, octal and any base up to 36.

Formulas

Repeated division by 2

n=2q+r,r∈{0,1}n = 2q + r, \quad r \in \{0, 1\}

Plain-text formula: n = 2q + r, r ∈ {0, 1}

qq
the quotient, carried into the next division
rr
the remainder, which is the next binary digit

Divide n by 2 to get a quotient q and a remainder r. The remainder is the next bit, starting from the right. Repeat with q until it is 0. The bits come out from least significant to most significant, which is why the remainders are read from the bottom up.

Binary place values

n=bk⋅2k+⋯+b1⋅21+b0⋅20n = b_{k} \cdot 2^{k} + \cdots + b_1 \cdot 2^1 + b_0 \cdot 2^0

Plain-text formula: n = b_k · 2^k + ⋯ + b_1 · 2^1 + b_0 · 2^0

bib_i
the bit in position i, counted from 0 on the right

Each bit is 0 or 1 and multiplies a power of 2. Writing a number in binary means finding which powers of 2 add up to it, which is what the subtraction method does directly.

Fraction bits: repeated multiplication by 2

2f=b−1+f1,0≤f1<12f = b_{-1} + f_1, \quad 0 \le f_1 < 1

Plain-text formula: 2f = b_(−1) + f_1, 0 ≤ f_1 < 1

For the part after the point, multiply by 2. The whole part of the product, 0 or 1, is the next bit after the point; continue with the fraction that is left. Stop when it reaches 0, or when a fraction repeats, in which case the bits repeat too.

Two’s complement of a negative number

pattern(−m)=2w−m\text{pattern}(-m) = 2^{w} - m

Plain-text formula: pattern(−m) = 2^w − m

mm
the size of the negative number, without its sign
ww
the bit width: 8, 16, 32 or 64

In a w-bit word, a negative number −m is stored as the binary form of 2^w − m. The same pattern comes from writing m in w bits, inverting every bit and adding 1.

Reading the result

The main result is the binary form of your number. With No padding, it has no leading zeros, so 13 is 1101. The panel also shows the same number padded to the smallest standard width, its hexadecimal and octal forms, and how many bits it needs: a whole number n needs one more bit than the highest power of 2 that fits in it.

With a bit width chosen, the answer always has exactly that many bits. For a positive number, the extra bits are leading zeros and do not change its value. For a negative number, the answer is the two’s complement pattern a computer stores: its top bit is 1, and the same bits read as unsigned give 2w2^w minus the number’s size, so −5 in 8 bits reads as 251 unsigned.

How to convert decimal to binary

Divide the number by 2 and write down the remainder, which is always 0 or 1. Divide the quotient by 2 again, and keep going until the quotient is 0. The remainders, read from the last one up to the first, are the binary digits. The first remainder is the rightmost bit because it says whether the number is odd. In UK courses decimal is often called denary, so “denary to binary” means the same conversion.

For 13: 13 ÷ 2 = 6 remainder 1, 6 ÷ 2 = 3 remainder 0, 3 ÷ 2 = 1 remainder 1, and 1 ÷ 2 = 0 remainder 1. Reading the remainders from the bottom up gives 1101. Check: 8 + 4 + 1 = 13.

Converting 13 to binary by repeated division by 2
DivideQuotientRemainderBit
13 ÷ 2612⁰ (rightmost)
6 ÷ 2302¹
3 ÷ 2112²
1 ÷ 2012³ (leftmost)

The subtraction method: powers of 2

For numbers up to a few hundred, it is often faster to subtract powers of 2. Find the largest power of 2 that fits, write a 1 in its place, subtract it, and repeat with what is left. Every place you skip gets a 0.

For 224: 128 fits (224 − 128 = 96), 64 fits (96 − 64 = 32), 32 fits (32 − 32 = 0), and nothing is left, so the 16, 8, 4, 2 and 1 places are 0. That gives 224 = 11100000. For 25: 16 fits (9 left), 8 fits (1 left), 4 and 2 do not, and 1 fits, so 25 = 11001.

Building 224 from powers of 2
Place value1286432168421
Bit for 22411100000
Bit for 2500011001

Decimal to binary table (0 to 63)

The table lists every number from 0 to 63, which covers all 6-bit values. Each extra bit doubles the range: 7 bits reach 127 and a full byte of 8 bits reaches 255. For any other number, type it into the converter above.

Decimal numbers 0 to 63 and their binary form
DecimalBinaryDecimalBinary
0032100000
1133100001
21034100010
31135100011
410036100100
510137100101
611038100110
711139100111
8100040101000
9100141101001
10101042101010
11101143101011
12110044101100
13110145101101
14111046101110
15111147101111
161000048110000
171000149110001
181001050110010
191001151110011
201010052110100
211010153110101
221011054110110
231011155110111
241100056111000
251100157111001
261101058111010
271101159111011
281110060111100
291110161111101
301111062111110
311111163111111

Common values in binary and hex

These are the values people look up most often, grouped in fours so they are easy to read and to turn into hexadecimal: each group of 4 bits is one hex digit. A number made of n ones, such as 1111 1111, is always 2n−12^n - 1, here 255, and a 1 followed by n zeros is 2n2^n.

Common decimal numbers in binary and hexadecimal
DecimalBinaryHex
64100 000040
100110 010064
127111 11117F
1281000 000080
1921100 0000C0
2001100 1000C8
2241110 0000E0
2401111 0000F0
2501111 1010FA
2551111 1111FF
2561 0000 0000100
5001 1111 01001F4
51210 0000 0000200
100011 1110 10003E8
102311 1111 11113FF
1024100 0000 0000400

Padding to 8, 16, 32 or 64 bits

Computers store numbers in fixed-size words: a byte is 8 bits, and common integer types are 16, 32 and 64 bits. Padding writes the number at that width by adding zeros on the left, which never changes the value: 13 is 1101, or 0000 1101 as a byte, or 0000 0000 0000 1101 as 16 bits.

A number only fits if it needs no more bits than the width allows. An unsigned 8-bit byte holds 0 to 255, 16 bits hold 0 to 65,535 and 32 bits hold 0 to 4,294,967,295. The converter warns you when a number does not fit, such as 256 in 8 bits, which needs 9.

Negative numbers: two’s complement

Binary has no minus sign inside a computer. Instead, signed integers use two’s complement: write the size of the number in the chosen width, invert every bit, and add 1. For −5 in 8 bits: 5 = 0000 0101, inverted = 1111 1010, plus 1 = 1111 1011. A shortcut gives the same answer: 28−5=2512^8 - 5 = 251, and 251 in binary is 1111 1011.

The width matters. −5 is 1111 1011 in 8 bits but 1111 1111 1111 1011 in 16 bits, because the extra bits copy the sign. A signed 8-bit number runs from −128 (1000 0000) to 127 (0111 1111). Written on paper without a fixed width, −5 is simply −101, and that is what the converter shows with no padding.

Negative numbers in 8-bit two’s complement
DecimalSize in binaryInvertedAdd 1 (8-bit pattern)
−10000 00011111 11101111 1111
−50000 01011111 10101111 1011
−160001 00001110 11111111 0000
−1000110 01001001 10111001 1100
−1281000 00000111 11111000 0000

Decimal fractions to binary

For the part after the decimal point, multiply by 2 instead of dividing. The whole part of each product is the next bit after the binary point. For 0.625: 0.625 × 2 = 1.25, bit 1; 0.25 × 2 = 0.5, bit 0; 0.5 × 2 = 1, bit 1, and nothing is left. So 0.625 = 0.101, and 156.625 = 10011100.101.

Most decimal fractions never end in binary. 0.1 gives 0.2, 0.4, 0.8, 1.6, 1.2, 0.4 … and the bits 0011 repeat forever: 0.1 = 0.0(0011). A computer has to cut that off, which is why 0.1 + 0.2 is not exactly 0.3 in most programming languages. Only fractions whose denominator is a power of 2, such as ½, ¼ or ⅜, end in binary. Read more about floating point and rounding.

Decimal to binary in Python, C, JavaScript and Excel

In Python, bin(13) returns 0b1101, and format(13, "08b") returns the padded byte 00001101. For a negative number in two’s complement, mask it to the width first: format(-5 & 0xFF, "08b") returns 11111011. Python’s bin(-5) gives -0b101, with a minus sign instead.

In JavaScript, (13).toString(2) returns 1101 and (13).toString(2).padStart(8, "0") pads it. (-5 >>> 0).toString(2) gives the 32-bit two’s complement pattern. In C there is no standard binary format before C23, which adds %b to printf; on older compilers, loop over the bits with (n >> i) & 1 from the top bit down.

In Excel and Google Sheets, =DEC2BIN(13) returns 1101 and =DEC2BIN(13, 8) returns 00001101. DEC2BIN accepts −512 to 511 only, and returns negatives as 10-bit two’s complement: =DEC2BIN(-5) is 1111111011. Use this converter for larger numbers or other widths.

Common mistakes

Reading the remainders from the top down. The first remainder is the rightmost bit. Read from the top and 13 comes out as 1011, which is 11.

Stopping too early. Keep dividing until the quotient is 0. The last division, 1 ÷ 2 = 0 remainder 1, gives the leading 1 and is easy to miss.

Dropping zeros in the middle. Every place between the leading 1 and the end needs a bit. 224 is 11100000, not 111.

Mixing up the methods for fractions. Divide the whole part but multiply the fractional part, and read the fraction bits from the top down, the opposite order to the whole part.

Practice questions with answers

Convert each number to binary, then check your answer in the table or by typing it into the converter, which shows every step.

Decimal to binary practice questions and answers
QuestionDecimalBinaryWorking
161104 + 2
2191001116 + 2 + 1
34210101032 + 8 + 2
4100110010064 + 32 + 4
520011001000128 + 64 + 8
610001111101000512 + 256 + 128 + 64 + 32 + 8
70.750.110.5 + 0.25
8−8 (8-bit)1111 1000256 − 8 = 248

Worked example

Convert 156 from decimal to binary

Write the decimal number 156 in binary, then as an 8-bit byte.

  1. Divide by 2 and keep the remainders: 156 ÷ 2 = 78 r 0, 78 ÷ 2 = 39 r 0, 39 ÷ 2 = 19 r 1, 19 ÷ 2 = 9 r 1.
  2. Keep going until the quotient is 0: 9 ÷ 2 = 4 r 1, 4 ÷ 2 = 2 r 0, 2 ÷ 2 = 1 r 0, 1 ÷ 2 = 0 r 1.
  3. Read the remainders from the bottom up: 1, 0, 0, 1, 1, 1, 0, 0, which gives 10011100.
  4. Check with place values: 128 + 16 + 8 + 4 = 156. The number already has 8 bits, so as a byte it is 1001 1100.

Answer: 156₁₀ = 10011100₂. In hexadecimal it is 9C, since 1001 = 9 and 1100 = C.

Frequently asked questions

How do you convert decimal to binary?

Divide the number by 2 repeatedly and write down each remainder until the quotient is 0. The remainders read from the bottom up are the binary number. For 13: remainders 1, 0, 1, 1 read upward give 1101.

What is 224 in binary?

224 is 11100000 in binary: 128 + 64 + 32 = 224, so the three highest places of a byte are 1 and the rest are 0. In hexadecimal it is E0. As a subnet mask octet, 224 means the first three bits are network bits.

What is 25 in binary?

25 is 11001 in binary, because 25 = 16 + 8 + 1. As an 8-bit byte it is 0001 1001.

What is 2 in binary?

2 is 10 in binary: a 1 in the 2s place and a 0 in the 1s place. Likewise 4 is 100, 8 is 1000 and 16 is 10000; every power of 2 is a 1 followed by zeros.

How do you write a negative number in binary?

With a fixed width, use two’s complement: write the size in that many bits, invert every bit and add 1. −5 in 8 bits is 1111 1011. Without a fixed width, write a minus sign in front: −101.

Why does 0.1 repeat in binary?

A decimal fraction ends in binary only if its denominator, in lowest terms, is a power of 2. 0.1 is 1/10, and 10 has a factor of 5, so the bits 0011 repeat forever: 0.1 = 0.0001100110011… in binary.

How many bits does a number need?

A positive whole number n needs ⌊log₂ n⌋ + 1 bits. So 255 needs 8 bits and 256 needs 9. The converter lists the bit count under the answer.

How do you convert binary back to decimal?

Add the place values 1, 2, 4, 8, 16, … wherever the bit is 1: 1101 = 8 + 4 + 1 = 13. The binary to decimal converter shows this bit by bit.

Binary to Decimal Converter

Binary to decimal with each bit’s power of 2 and a running total, signed or unsigned, fractions included.

Number Theory & Bases

Hex to Decimal Converter

Hex to decimal with place-value steps, plus binary, octal and any base 2 to 36, fractions included.

Number Theory & Bases

Binary Calculator

Binary addition, subtraction and bitwise operations with steps, at 8 to 64 bits, signed or unsigned.

Number Theory & Bases

Mod Calculator

a mod n with steps, negative numbers, power mod and modular inverses.

Number Theory & Bases

Scientific Calculator

Type or tap expressions with exact fractions, memory, variables, constants and complex mode.

Tools

Sources and further reading

  1. The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd edition, §4.4 Radix Conversion, Addison-Wesley (Donald E. Knuth, 1997)
  2. Computer Systems: A Programmer’s Perspective, 3rd edition, §2.2 Integer Representations (unsigned and two’s complement encodings), Pearson (Randal E. Bryant and David R. O’Hallaron, 2016)
  3. Python documentation: bin() and the format specification mini-language (opens in a new tab), Python Software Foundation
  4. DEC2BIN function (opens in a new tab), Microsoft Support
  5. Number.prototype.toString() (opens in a new tab), MDN Web Docs (Mozilla)

Results are calculated in your browser with methods checked by automated tests, and are intended for learning and general use. For engineering, medical, financial, legal or safety-critical decisions, verify important values independently and follow the rounding rules your work requires. How accurate are these results?