# Negative base

Part of a series on :
Numeral systems :
Place-value notation
Hindu–Arabic numerals
Western Arabic
Eastern Arabic
Bengali
Devanagari
Gujarati
Gurmukhi
Odia
Sinhala
Tamil
Malayalam
Telugu
Kannada
Dzongkha
Tibetan
Balinese
Burmese
Javanese
Khmer
Lao
Mongolian
Sundanese
Thai
East Asian systems
Contemporary
Chinese
Suzhou
Hokkien
Japanese
Korean
Vietnamese
Historic
Counting rods
Tangut
Other systems
History
Ancient
Babylonian
Post-classical
Cistercian
Mayan
Muisca
Pentadic
Quipu
Rumi
Contemporary
Cherokee
Kaktovik (Iñupiaq)
By radix/base
Common radices/bases
2
3
4
5
6
8
10
12
16
20
60
Non-standard radices/bases
Bijective (1)
Signed-digit (balanced ternary)
Mixed (factorial)
Negative
Complex (2i)
Non-integer (φ)
Asymmetric :
Sign-value notation
Non-alphabetic
Aegean
Attic
Aztec
Brahmi
Chuvash
Egyptian
Etruscan
Kharosthi
Prehistoric counting
Proto-cuneiform
Roman
Tally marks
Alphabetic
Abjad
Armenian
Alphasyllabic
Akṣarapallī
Āryabhaṭa
Kaṭapayādi
Coptic
Cyrillic
Geʽez
Georgian
Glagolitic
Greek
Hebrew :
List of numeral systems :
vte :
A negative base (or negative radix) may be used to construct a non-standard positional numeral system. Like other place-value systems, each position holds multiples of the appropriate power of the system's base; but that base is negative—that is to say, the base b is equal to −r for some natural number r (r ≥ 2).
Negative-base systems can accommodate all the same numbers as standard place-value systems, but both positive and negative numbers are represented without the use of a minus sign (or, in computer representation, a sign bit); this advantage is countered by an increased complexity of arithmetic operations. The need to store the information normally contained by a negative sign often results in a negative-base number being one digit longer than its positive-base equivalent.
The common names for negative-base positional numeral systems are formed by prefixing nega- to the name of the corresponding positive-base system; for example, negadecimal (base −10) corresponds to decimal (base 10), negabinary (base −2) to binary (base 2), negaternary (base −3) to ternary (base 3), and negaquaternary (base −4) to quaternary (base 4).

## Example

Consider what is meant by the representation 12243 in the negadecimal system, whose base b is −10:
The representation 12,243$_{−10}$ (which is intended to be negadecimal notation) is equivalent to 8,163$_{10}$ in decimal notation, because 10,000 + (−2,000) + 200 + (−40) + 3 = 8,163.

### Remark

On the other hand, −8,163$_{10}$ in decimal would be written 9,977$_{−10}$ in negadecimal.

## History

Negative numerical bases were first considered by Vittorio Grünwald in an 1885 monograph published in Giornale di Matematiche di Battaglini. Grünwald gave algorithms for performing addition, subtraction, multiplication, division, root extraction, divisibility tests, and radix conversion. Negative bases were later mentioned in passing by A. J. Kempner in 1936 and studied in more detail by Zdzisław Pawlak and A. Wakulicz in 1957.
Negabinary was implemented in the early Polish computer BINEG (and UMC), built 1957–59, based on ideas by Z. Pawlak and A. Lazarkiewicz from the Mathematical Institute in Warsaw. Implementations since then have been rare.
zfp, a floating-point compression algorithm from the Lawrence Livermore National Laboratory, uses negabinary to store numbers. According to zfp's documentation:
   Unlike sign-magnitude representations, the leftmost one-bit in negabinary simultaneously encodes the sign and approximate magnitude of a number. Moreover, unlike two’s complement, numbers small in magnitude have many leading zeros in negabinary regardless of sign, which facilitates encoding.

## Notation and use

Denoting the base as −r, every integer a can be written uniquely as
    $a=\sum _{i=0}^{n}d_{i}(-r)^{i}$
where each digit d$_k$ is an integer from 0 to r − 1 and the leading digit d$_n$ > 0 (unless n = 0). The base −r expansion of a is then given by the string d$_n$d$_{n−1}$...d$_1$d$_0$.
Negative-base systems may thus be compared to signed-digit representations, such as balanced ternary, where the radix is positive but the digits are taken from a partially negative range. (In the table below the digit of value −1 is written as the single character T.)
Some numbers have the same representation in base −r as in base r. For example, the numbers from 100 to 109 have the same representations in decimal and negadecimal. Similarly,
    $17=2^{4}+2^{0}=(-2)^{4}+(-2)^{0}$
and is represented by 10001 in binary and 10001 in negabinary.
Some numbers with their expansions in a number of positive and corresponding negative bases are:
| Decimal | Negadecimal | Binary | Negabinary | Ternary | Negaternary | Balanced Ternary | Balanced Negaternary | Quaternary | Negaquaternary |
| −15 | 25 | −1111 | 110001 | −120 | 1220 | T110 | 11T0 | −33 | 1301 |
| −5 | 15 | −101 | 1111 | −12 | 21 | T11 | TT1 | −11 | 23 |
| −4 | 16 | −100 | 1100 | −11 | 22 | TT | 1T | −10 | 10 |
| −3 | 17 | −11 | 1101 | −10 | 10 | T0 | 10 | −3 | 11 |
| −2 | 18 | −10 | 10 | −2 | 11 | T1 | 11 | −2 | 12 |
| −1 | 19 | −1 | 11 | −1 | 12 | T | T | −1 | 13 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 2 | 2 | 10 | 110 | 2 | 2 | 1T | TT | 2 | 2 |
| 3 | 3 | 11 | 111 | 10 | 120 | 10 | T0 | 3 | 3 |
| 4 | 4 | 100 | 100 | 11 | 121 | 11 | T1 | 10 | 130 |
| 5 | 5 | 101 | 101 | 12 | 122 | 1TT | 11T | 11 | 131 |
| 6 | 6 | 110 | 11010 | 20 | 110 | 1T0 | 110 | 12 | 132 |
| 7 | 7 | 111 | 11011 | 21 | 111 | 1T1 | 111 | 13 | 133 |
| 8 | 8 | 1000 | 11000 | 22 | 112 | 10T | 10T | 20 | 120 |
| 9 | 9 | 1001 | 11001 | 100 | 100 | 100 | 100 | 21 | 121 |
| 10 | 190 | 1010 | 11110 | 101 | 101 | 101 | 101 | 22 | 122 |
| 11 | 191 | 1011 | 11111 | 102 | 102 | 11T | 1TT | 23 | 123 |
| 12 | 192 | 1100 | 11100 | 110 | 220 | 110 | 1T0 | 30 | 110 |
| 13 | 193 | 1101 | 11101 | 111 | 221 | 111 | 1T1 | 31 | 111 |
| 14 | 194 | 1110 | 10010 | 112 | 222 | 1TTT | TT1T | 32 | 112 |
| 15 | 195 | 1111 | 10011 | 120 | 210 | 1TT0 | TT10 | 33 | 113 |
| 16 | 196 | 10000 | 10000 | 121 | 211 | 1TT1 | TT11 | 100 | 100 |
| 17 | 197 | 10001 | 10001 | 122 | 212 | 1T0T | TT0T | 101 | 101 |
| 18 | 198 | 10010 | 10110 | 200 | 200 | 1T00 | TT00 | 102 | 102 |
Note that, with the exception of nega balanced ternary, the base −r expansions of negative integers have an even number of digits, while the base −r expansions of the non-negative integers have an odd number of digits.

## Calculation

The base −r expansion of a number can be found by repeated division by −r, recording the non-negative remainders in $\{0,1,\ldots ,r-1\}$, and concatenating those remainders, starting with the last. Note that if a / b is c with remainder d, then bc + d = a and therefore d = a − bc. To arrive at the correct conversion, the value for c must be chosen such that d is non-negative and minimal. For the fourth line of the following example this means that
    $-5\div (-3)=2~\mathrm {remainder} ~1$
has to be chosen — and not $=3~\mathrm {remainder} ~4$ nor $=1~\mathrm {remainder} ~-\!2.$
For example, to convert 146 in decimal to negaternary:
    ${\begin{array}{rr}146\div (-3)=&-48~\mathrm {remainder} ~2\\-48\div (-3)=&16~\mathrm {remainder} ~0\\16\div (-3)=&-5~\mathrm {remainder} ~1\\-5\div (-3)=&2~\mathrm {remainder} ~1\\2\div (-3)=&0~\mathrm {remainder} ~2\end{array}}$
Reading the remainders backward we obtain the negaternary representation of 146$_{10}$: 21102$_{–3}$.
    Proof: -3 · (-3 · (-3 · (-3 · ( 2 ) + 1 ) + 1 ) + 0 ) + 2 = (((2 · (–3) + 1) · (–3) + 1) · (–3) + 0) · (–3) + 2
    = 146$_{10}$.
Reading the remainders forward we can obtain the negaternary least-significant-digit-first representation.
    Proof: 2 + ( 0 + ( 1 + ( 1 + ( 2 ) · -3 ) · -3) · -3 ) · -3  = 146$_{10}$.
Note that in most programming languages, the result (in integer arithmetic) of dividing a negative number by a negative number is rounded towards 0, usually leaving a negative remainder. In such a case we have a = (−r)c + d = (−r)c + d − r + r = (−r)(c + 1) + (d + r). Because |d| < r, (d + r) is the positive remainder. Therefore, to get the correct result in such case, computer implementations of the above algorithm should add 1 and r to the quotient and remainder respectively.

### Example implementation code
#### To any negative base
##### Java

The above gives the result in an ArrayList of integers, so that the code does not have to handle how to represent a base smaller than -10. To display the result as a string, one can decide on a mapping of base to characrters. For example:

##### AutoLisp
### Shortcut calculation

The following algorithms assume that
* the input is available in bitstrings and coded in (base +2; digits in $\{0,1\}$) (as in most of today's digital computers),
* there are add (+) and xor (^) operations which operate on such bitstrings (as in most of today's digital computers),
* the set $D$ of output digits is standard, i. e. $D=\{0,|b|-1\}$ with base $b\in \{-2,-4\}$,
* the output is coded in the same bitstring format, but the meaning of the places is another one.

#### To negabinary

The conversion to negabinary (base −2; digits in $\{0,1\}$) allows a remarkable shortcut (C implementation):
JavaScript port for the same shortcut calculation:
The algorithm is first described by Schroeppel in the HAKMEM (1972) as item 128. The Wolfram MathWorld documents a version in the Wolfram Language by D. Librik (Szudzik).

#### To negaquaternary

The conversion to negaquaternary (base −4; digits in $\{0,1,2,3\}$) allows a similar shortcut (C implementation):
JavaScript port for the same shortcut calculation:

## Arithmetic operations

The following describes the arithmetic operations for negabinary; calculations in larger bases are similar.

### Addition

Adding negabinary numbers proceeds bitwise, starting from the least significant bits; the bits from each addend are summed with the (balanced ternary) carry from the previous bit (0 at the LSB). This sum is then decomposed into an output bit and carry for the next iteration as show in the table:
| Sum | Sum | Output | Output | Output | Comment |
| Sum | Sum | Bit | Carry | Carry | | Comment |
| −2 | 010$_{−2}$ | 0 | 1 | 01$_{−2}$ | −2 occurs only during subtraction. |
| −1 | 011$_{−2}$ | 1 | 1 | 01$_{−2}$ | |
| 0 | 000$_{−2}$ | 0 | 0 | 00$_{−2}$ | |
| 1 | 001$_{−2}$ | 1 | 0 | 00$_{−2}$ | |
| 2 | 110$_{−2}$ | 0 | −1 | 11$_{−2}$ | |
| 3 | 111$_{−2}$ | 1 | −1 | 11$_{−2}$ | 3 occurs only during addition. |
The second row of this table, for instance, expresses the fact that −1 = 1 + 1 × −2; the fifth row says 2 = 0 + −1 × −2; etc.
As an example, to add 1010101$_{−2}$ (1 + 4 + 16 + 64 = 85) and 1110100$_{−2}$ (4 + 16 − 32 + 64 = 52),
so the result is 110011001$_{−2}$ (1 − 8 + 16 − 128 + 256 = 137).

#### Another method

While adding two negabinary numbers, every time a carry is generated an extra carry should be propagated to next bit. Consider same example as above

#### Negabinary full adder

A full adder circuit can be designed to add numbers in negabinary. The following logic is used to calculate the sum and carries:
    $s_{i}=a_{i}\oplus b_{i}\oplus c_{i}^{+}\oplus c_{i}^{-}$
    $c_{i+1}^{+}={\overline {a_{i}}}{\overline {b_{i}}}{\overline {c_{i}^{+}}}c_{i}^{-}$
    $c_{i+1}^{-}=a_{i}b_{i}{\overline {c_{i}^{-}}}+(a_{i}\oplus b_{i})c_{i}^{+}{\overline {c_{i}^{-}}}$

#### Incrementing negabinary numbers

Incrementing a negabinary number can be done by using the following formula:
    $2x\oplus ((2x\oplus x)+1)$
(The operations in this formula are to be interpreted as operations on regular binary numbers. For example, $2x$ is a binary left shift by one bit.)

### Subtraction

To subtract, multiply each bit of the second number by −1, and add the numbers, using the same table as above.
As an example, to compute 1101001$_{−2}$ (1 − 8 − 32 + 64 = 25) minus 1110100$_{−2}$ (4 + 16 − 32 + 64 = 52),
so the result is 100101$_{−2}$ (1 + 4 −32 = −27).
Unary negation, −x, can be computed as binary subtraction from zero, 0 − x.

### Multiplication and division

Shifting to the left multiplies by −2, shifting to the right divides by −2.
To multiply, multiply like normal decimal or binary numbers, but using the negabinary rules for adding the carry, when adding the numbers.
For each column, add carry to number, and divide the sum by −2, to get the new carry, and the resulting bit as the remainder.

### Comparing negabinary numbers

It is possible to compare negabinary numbers by slightly adjusting a normal unsigned binary comparator. When comparing the numbers $A$ and $B$, invert each odd positioned bit of both numbers. After this, compare $A$ and $B$ using a standard unsigned comparator.

## Fractional numbers

Base −r representation may of course be carried beyond the radix point, allowing the representation of non-integer numbers.
As with positive-base systems, terminating representations correspond to fractions where the denominator is a power of the base; repeating representations correspond to other rationals, and for the same reason.

### Non-unique representations

Unlike positive-base systems, where integers and terminating fractions have non-unique representations (for example, in decimal 0.999... = 1) in negative-base systems the integers have only a single representation. However, there do exist rationals with non-unique representations. For the digits {0, 1, ..., t} with $\mathbf {t} :=r\!-\!\!1=-b\!-\!\!1$ the biggest digit and
    $T:=0.{\overline {01}}_{b}=\sum _{i=1}^{\infty }b^{-2i}={\frac {1}{b^{2}-1}}={\frac {1}{r^{2}-1}}$
we have
    $0.{\overline {0\mathbf {t} }}_{b}=\mathbf {t} T={\frac {r\!-\!\!1}{r^{2}-1}}={\frac {1}{r+1}}$  as well as
    $1.{\overline {\mathbf {t} 0}}_{b}=1+\mathbf {t} bT={\frac {(r^{2}-1)+(r\!-\!\!1)b}{r^{2}-1}}={\frac {1}{r+1}}.$
So every number ${\frac {1}{r+1}}+z$ with a terminating fraction $z\in \mathbb {Z} r^{\mathbb {Z} }$ added has two distinct representations.
For example, in negaternary, i.e. $b=-3$ and $\mathbf {t} =2$, there is
    $1.{\overline {02}}_{(-3)}={\frac {5}{4}}=2.{\overline {20}}_{(-3)}$.
Such non-unique representations can be found by considering the largest and smallest possible representations with integer parts 0 and 1 respectively, and then noting that they are equal. (Indeed, this works with any integer-base system.) The rationals thus non-uniquely expressible are those of form
    ${\frac {z(r+1)+1}{b^{i}(r+1)}}$
with $z,i\in \mathbb {Z} .$

## Imaginary base

Just as using a negative base allows the representation of negative numbers without an explicit negative sign, using an imaginary base allows the representation of Gaussian integers. Donald Knuth proposed the quater-imaginary base (base 2i) in 1955.