A binary expansion of a nonnegative real number is a base-2
representation
where
is a nonnegative integer and each binary
digit
is either 0 or 1. For example,
,
, and
.
Negative numbers are not covered by this definition. Their representation requires an additional convention. In ordinary mathematical
notation, a minus sign is often placed before a binary numeral. For example,
. This is a notation convention, not part
of the positional expansion above. In contrast, negabinary,
which uses base
, represents every integer
uniquely by a finite string of the digits 0 and 1 and requires
no separate sign.
A fixed-width computer representation of a negative integer instead requires an encoding convention. Common choices are sign-and-magnitude representation, ones' complement, and two's complement.
A nonnegative rational number has an eventually periodic binary expansion, and every eventually
periodic binary expansion represents a nonnegative rational
number. If
is a reduced fraction, then it has a terminating
binary expansion iff
is a power of 2.
For , long division
starts with the remainder after dividing
by
. Each step uses integer
division of twice the current remainder by
. The quotient is the next
binary digit, and the new remainder
determines the next step. A zero remainder gives a
terminating expansion. Otherwise there are only finitely many possible remainders,
so a repeated remainder starts a periodic block. Conversely,
a repeating block contributes a geometric series
with a rational sum. A terminating expansion with
fractional digits has denominator dividing
in lowest terms. If
, moving the binary point in the base-2 representation
of
by
places to the left gives a terminating expansion.
Every positive real number with a terminating binary expansion also has a nonterminating binary expansion whose
binary digits are eventually all 1s. For example,
. Apart from leading zeros,
pairs of this form account for all nonuniqueness of binary expansions.
For , the Mersenne
number
has binary expansion
with
binary digits, all equal to 1, and is therefore a binary repunit.
The binary expansion of a number can be obtained in the Wolfram
Language using RealDigits[x,
2].