How to Convert a Number to Binary
At a glance
- 1. Split the number
Separate its sign, integer part, and fractional part.
- 2. Convert the integer
Divide by 2 repeatedly and read the remainders in reverse.
- 3. Convert the fraction
Multiply by 2 repeatedly and record each integer part.
- 4. Choose the format
Stop at an exact result or a chosen precision; use IEEE 754 rules for a machine float.
A number can be written in binary as an ordinary numeral, such as , or as the bit pattern stored by a floating-point type. Those are related but different tasks. The repeated-division and repeated-multiplication method gives the binary expansion; to get a machine float’s bits, you must also apply the format’s exponent and rounding rules.
The basic conversion algorithm
Convert the integer and fractional parts separately. For the integer part, repeatedly divide by 2 and record each remainder. The remainders appear from least significant to most significant, so read them backward. If the integer part is zero, its representation is simply 0.
For the fractional part, repeatedly multiply by 2. The integer part of each result is the next binary digit after the point. Keep only the fractional remainder and repeat. In exact arithmetic, stop when the remainder reaches zero. If it does not, stop after a chosen number of digits and indicate that the result is truncated or rounded.
A compact version of the procedure is:
convert(x, maximumFractionBits):
sign = '-' if x < 0, otherwise ''
a = abs(x)
n = floor(a)
f = a - n
integerBits = '0' if n == 0, otherwise ''
while n > 0:
prepend (n mod 2) to integerBits
n = floor(n / 2)
fractionBits = ''
while f != 0 and length(fractionBits) < maximumFractionBits:
f = 2 * f
if f >= 1:
append '1' to fractionBits
f = f - 1
else:
append '0' to fractionBits
return sign + integerBits + '.' + fractionBits
For exact results, represent the input as a rational number while performing the fractional steps. Ordinary floating-point arithmetic can introduce rounding into the steps themselves. The maximumFractionBits limit is also necessary when the expansion does not terminate.
Worked example: convert 13.625
First convert the integer part, 13:
| Division | Remainder |
|---|---|
| 13 / 2 = 6 remainder | 1 |
| 6 / 2 = 3 remainder | 0 |
| 3 / 2 = 1 remainder | 1 |
| 1 / 2 = 0 remainder | 1 |
Read the remainders upward: .
Now convert the fractional part, 0.625:
| Multiply by 2 | Next bit | New fractional remainder |
|---|---|---|
| 0.625 × 2 = 1.25 | 1 | 0.25 |
| 0.25 × 2 = 0.5 | 0 | 0.5 |
| 0.5 × 2 = 1.0 | 1 | 0 |
The fractional digits are .101, so the complete result is . You can check it by place value: .
Getting an IEEE 754 bit pattern
A floating-point bit pattern is not just the digits in the binary expansion. In a common IEEE 754 format, a normal finite value is stored as a sign bit, a biased exponent, and a fraction field. The leading 1 in the normalized binary significand is implicit, so it is not stored in the fraction field.
For the example, normalize the binary value: . In IEEE 754 binary32 (single precision), the sign is 0, the exponent bias is 127, and the fraction field has 23 bits. The stored exponent is , or 10000010 in binary. The fraction field starts with 101101 and is padded with zeros to 23 bits.
The complete binary32 pattern is:
0 | 10000010 | 10110100000000000000000
The three groups are the sign, exponent, and fraction fields. Binary64 (double precision) uses a different exponent bias and a 52-bit fraction field, so its bit pattern differs even for the same value.
If the value cannot be represented exactly, the format rounds it to a representable value. IEEE 754 normally uses round to nearest, ties to even, unless a program or environment selects a different rounding mode. Very small values may be subnormal; zero, infinity, and NaN also have special encodings. These cases are handled by the format rules rather than by the simple normal-number procedure above.
Why some binary expansions repeat
A fraction has a terminating binary expansion exactly when its reduced denominator is a power of 2. For example, terminates because its denominator is . But does not: its denominator has a factor of 5, so its binary expansion repeats. You can still generate as many leading bits as needed, but you cannot write the exact value with finitely many binary fractional digits.
This matters when converting decimal input. The decimal text 0.1 denotes exactly as a mathematical decimal, but a typical binary floating-point type cannot store that value exactly. The stored float is a nearby representable value, and printing it as decimal may hide that fact.
Common mistakes
- Reading remainders in the wrong order: integer remainders must be reversed; fractional digits are recorded in the order produced.
- Assuming every fraction terminates: some binary expansions repeat forever. Set a precision limit or track a repeating remainder.
- Confusing the expansion with the stored bits: IEEE 754 adds sign and exponent fields and may round the value.
- Ignoring negative zero and special values: the simple sign-and-digits method does not describe every IEEE 754 encoding.
- Using inexact arithmetic for an exact conversion: use rational arithmetic when the input is an exact decimal or fraction and you need its exact binary expansion.
References
- IEEE Standard for Floating-Point Arithmetic (IEEE 754-2019)
- What Every Computer Scientist Should Know About Floating-Point Arithmetic — David Goldberg
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.