Binary to decimal converter
This binary to decimal converter reads base-2 numbers and gives you their familiar base-10 values. It is useful for decoding register values, bit masks, subnet masks and exam questions, and it copes with inputs that are hundreds of bits long without losing precision.
How binary place values work
In binary, each position is a power of two. The rightmost bit is worth 1, the next 2, then 4, 8, 16 and so on. To find the decimal value, add the place values of every position that holds a 1.
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| 10011100 | 1 | 0 | 0 | 1 | 1 | 1 | 0 | 0 |
For 10011100 the 1s sit at 128, 16, 8 and 4, so the value is 128 + 16 + 8 + 4 = 156.
The doubling method
For long strings there is a faster technique that avoids a table. Start at 0, then for each bit from left to right, double the running total and add the bit.
1011
start 0 → 0×2+1 = 1 → 1×2+0 = 2 → 2×2+1 = 5 → 5×2+1 = 11
This is exactly how computers parse numbers, and it works the same way for any base if you replace 2 with the base.
Binary fractions
Bits after the binary point are worth 1/2, 1/4, 1/8 and so on. The value 101.101 is therefore 4 + 1 + 0.5 + 0.125 = 5.625. Every binary fraction has a finite decimal expansion, so these results are always exact.
Example
Input Output
1010 10
11111111 255
100000000 256
101.101 5.625
0b1101 13
-1010 -10
Input rules worth knowing
- Spaces are ignored. If you paste grouped bits like
1010 1111or1010_1111, the tool reads them as one number and gives 175. To convert 1010 and 1111 separately (10 and 15), enter them one after the other. - Prefixes and signs: a leading
0bis accepted, and a minus sign gives a negative result. - Only 0 and 1 are valid. A value such as 1102 is flagged with the invalid digit highlighted, so you can spot typos quickly.
- Unlimited length: a string of sixty-four 1s converts to exactly 18446744073709551615, with no rounding.
Unsigned vs signed values
The converter reads every bit pattern as an unsigned number, so 11111111 is always 255. In a signed 8-bit variable the same pattern means −1, because two's complement gives the top bit a negative weight of −128. When the width matters, work in the bitwise calculator, which prints both the unsigned and the signed reading of a result.
Practical uses
- Networking: the mask 11111111.11111111.11111111.00000000 is 255.255.255.0. The binary to IP converter handles all four octets at once.
- Hardware and embedded work: datasheets describe control registers bit by bit, and you often need the decimal value to write in code.
- Learning: checking homework and practicing the doubling method.
For the opposite direction, use the decimal to binary converter. If you would rather have a short, readable form of the same bits, binary to hex packs every four bits into one digit.
Common mistakes
- Starting the place values at the left. Position 0, worth 1, is always the rightmost bit of the whole number part.
- Counting 2^8 as the value of the eighth bit. An 8-bit number uses positions 0 to 7, so its top bit is worth 128, not 256.
- Confusing binary numbers with binary-encoded text. The string 01001000 is 72 as a number, which happens to be the ASCII code of "H".
Binary to decimal in Python
Pass the binary digits as a string to int() with base 2: int("1010", 2) returns 10. The base is the part people forget; without it, int("1010") is one thousand and ten. A 0b prefix and underscores between digits are accepted, and because Python integers have no size limit, long bit strings convert exactly. int() does not understand a binary point, though: int("101.101", 2) raises ValueError, so fractions need a few lines that add up the negative powers of two.
print(int("1010", 2)) # 10
print(int("11111111", 2)) # 255
print(int("0b100000000", 2)) # 256, the 0b prefix is accepted
print(int("1111_0000", 2)) # 240, underscores are ignored
print(0b1010) # 10, a binary literal is already an int
def binary_to_decimal(text):
"""Binary string with an optional fraction, e.g. 101.101"""
whole, _, frac = text.partition(".")
value = int(whole or "0", 2)
for position, bit in enumerate(frac, start=1):
value += int(bit, 2) / 2 ** position
return value
print(binary_to_decimal("101.101")) # 5.625
The converter above handles fractions and long values as well, which makes it a quick check on your function; decimal to binary goes the other way.