Menu

Decimal to Binary Converter

Type a whole number and read it in binary, with each division by 2 shown underneath. It opens on 42, which is 101010.

By Nethanel Bar, Co-founder & CEO

Last updated

Ready to actually learn to code?

Coddy teaches you by writing real code in your browser - interactive lessons, instant feedback, and AI help when you get stuck.

How to convert decimal to binary

Every division by 2 leaves a remainder of 0 or 1, and that remainder is the next bit, starting from the right. For 42: 42 ÷ 2 = 21 remainder 0, 21 ÷ 2 = 10 remainder 1, 10 ÷ 2 = 5 remainder 0, 5 ÷ 2 = 2 remainder 1, 2 ÷ 2 = 1 remainder 0 and 1 ÷ 2 = 0 remainder 1. Read upward, the remainders give 101010.

A second method works well for small numbers: find the largest power of 2 that fits (32 for 42), subtract it and repeat with what is left (10, then 8, then 2). Put a 1 under each power you used and a 0 under the others.

The converter shows the division method for any whole number you type and uses BigInt, so large values stay exact.

What to know about writing numbers in binary

  • The last remainder you write down is the leftmost bit.
  • Even numbers end in 0 in binary and odd numbers end in 1.
  • Doubling a number adds a 0 to the end of its binary form: 21 is 10101 and 42 is 101010.
  • Numbers from 128 to 255 need exactly 8 bits; 256 is the first number that needs 9.

Decimal to binary, worked out

42

Decimal

42

Binary

101010

42 = 32 + 8 + 2, so the bits for 32, 8 and 2 are 1 and the others are 0.

255

Decimal

255

Binary

11111111

255 is one less than 256, so every bit below 256 is set: eight 1s.

A network mask byte

Decimal

192

Binary

11000000

192 = 128 + 64. Subnet masks such as 255.255.255.192 are read one byte at a time like this.

A power of two

Decimal

1024

Binary

10000000000

1024 is 2¹⁰, so it is a 1 followed by ten 0s.

Common decimal to binary mistakes

  • Reading the remainders from top to bottom. The first remainder is the rightmost bit, so read them from the last to the first.
  • Stopping one division early. Keep dividing until the quotient is 0; the final remainder of 1 is the leading bit.
  • Converting a negative number or a fraction the same way. Negative numbers use two's complement, and fractions need a separate method.
  • Losing the leading zeros a fixed width needs. 5 is 101, but as a byte it is 00000101.

Decimal to binary FAQ

What is 10 in binary?
1010. 10 = 8 + 2, so the bits for 8 and 2 are 1.
What is 100 in binary?
1100100. 100 = 64 + 32 + 4.
How do I convert decimal to binary in Python?
bin(42) returns '0b101010', and format(42, 'b') returns '101010' without the prefix. In JavaScript, use (42).toString(2).
How many bits does a number need?
Count the divisions by 2 until the quotient reaches 0; that count is the number of bits. 42 needs 6 and 255 needs 8.

Learn more

Other developer tools

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED