DEV Community

Thecodetranslator
Thecodetranslator

Posted on

How to Convert Binary to Decimal (Two Methods, Plus the Code Gotchas)

Binary to decimal is one of those things you learn once and then half-forget. Here is the short version, plus the parts that actually bite in code.

The place-value method

Write the place values under each digit, starting from the right: 1, 2, 4, 8, 16, 32. Keep the values under a 1, ignore the ones under a 0, and add them up.

Take 101101:

Digit 1 0 1 1 0 1
Place value 32 16 8 4 2 1
Digit × value 32 0 8 4 0 1

32 + 8 + 4 + 1 = 45.

The rightmost digit is worth 1 because it is 2⁰. Each step left doubles the weight, the same way each step left in a decimal number multiplies by 10.

The doubling method

No table needed. Go left to right: double your running total, then add the next digit.

For 101101 the totals run 1, 2, 5, 11, 22, 45. Same answer. Place values explain why it works. Doubling is faster for long numbers, and it is how most code does it.

Quick reference

  • 101 = 5
  • 1111 = 15 (the biggest value that fits in 4 bits)
  • 11001011 = 203
  • n bits hold 0 to 2ⁿ − 1: 255 for 8 bits, 65,535 for 16, 4,294,967,295 for 32

Fractions

Digits after the binary point have shrinking weights: ½, ¼, ⅛, and so on. 101.011 is 5 for the whole part, plus 0.25 + 0.125, which gives 5.375.

Signed binary: 251 or -5?

The same bits can mean two things. 11111011 is 251 as an unsigned number. In 8-bit two's complement the leftmost bit counts as -128, so it is -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5. A leading 1 only means negative when you read the number as signed.

In code

int('101101', 2)        # 45
int('0b101101', 0)      # 45
Enter fullscreen mode Exit fullscreen mode
parseInt('101101', 2)   // 45
Number('0b101101')      // 45
BigInt('0b' + '1'.repeat(64))  // 18446744073709551615n
Enter fullscreen mode Exit fullscreen mode
=BIN2DEC("1100100")     // Excel: 100
Enter fullscreen mode Exit fullscreen mode

Three gotchas:

  1. parseInt('0b101101', 2) returns 0. It stops at the b. Strip the prefix or use Number().
  2. parseInt is only exact up to 2⁵³ − 1. Sixty-four ones comes out as 18446744073709552000, not 18446744073709551615. Use BigInt for anything big.
  3. Excel's BIN2DEC takes at most 10 digits and treats the first as a sign bit, so =BIN2DEC(1111111111) returns -1, not 1023.

Common mistakes

  • Starting the powers at 2 instead of 1 (101 is 4 + 0 + 1 = 5, not 7).
  • Reversing the place values.
  • Treating a leading 1 as a minus sign outside signed mode.
  • Typing a digit that is not 0 or 1.

Check your work

I built a free converter that shows every step (place values and doubling), handles fractions and signed values up to 64 bits, and runs entirely in your browser, so nothing you type is sent anywhere:

Binary to Decimal Converter

It also has a 0–32 chart and five practice questions if you want to test yourself.

What trips you up most with binary: signed values, fractions, or the language quirks? Let me know in the comments.

Top comments (0)