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 is equal to for some natural number ().
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).
Consider what is meant by the representation in the negadecimal system, whose base is âÂÂ10:
The representation (which is intended to be negadecimal notation) is equivalent to in decimal notation, because 10,000 + (âÂÂ2,000) + 200 + (âÂÂ40) + 3 = .
On the other hand, in decimal would be written in negadecimal.
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: <blockquote>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.</blockquote>
Denoting the base as , every integer can be written uniquely as
where each digit is an integer from 0 to and the leading digit (unless ). The base expansion of is then given by the string .
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 as in base . For example, the numbers from 100 to 109 have the same representations in decimal and negadecimal. Similarly,
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:
Note that, with the exception of nega balanced ternary, the base expansions of negative integers have an even number of digits, while the base expansions of the non-negative integers have an odd number of digits.
The base expansion of a number can be found by repeated division by , recording the non-negative remainders in , and concatenating those remainders, starting with the last. Note that if is with remainder , then and therefore . To arrive at the correct conversion, the value for must be chosen such that is non-negative and minimal. For the fourth line of the following example this means that
has to be chosen â and not nor
For example, to convert 146 in decimal to negaternary:
Reading the remainders backward we obtain the negaternary representation of 146<sub>10</sub>: 21102<sub>âÂÂ3</sub>.
Reading the remainders forward we can obtain the negaternary least-significant-digit-first representation.
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 . Because , is the positive remainder. Therefore, to get the correct result in such case, computer implementations of the above algorithm should add 1 and to the quotient and remainder respectively.
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:
The following algorithms assume that
The conversion to negabinary (base âÂÂ2; digits in ) 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).
The conversion to negaquaternary (base âÂÂ4; digits in ) allows a similar shortcut (C implementation):
JavaScript port for the same shortcut calculation:
The following describes the arithmetic operations for negabinary; calculations in larger bases are similar.
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:
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 (1 + 4 + 16 + 64 = 85) and 1110100 (4 + 16 â 32 + 64 = 52),
Carry: 1 âÂÂ1 0 âÂÂ1 1 âÂÂ1 0 0 0 First addend: 1 0 1 0 1 0 1 Second addend: 1 1 1 0 1 0 0 + -------------------------- Number: 1 âÂÂ1 2 0 3 âÂÂ1 2 0 1 Bit (result): 1 1 0 0 1 1 0 0 1 Carry: 0 1 âÂÂ1 0 âÂÂ1 1 âÂÂ1 0 0
so the result is 110011001 (1 â 8 + 16 â 128 + 256 = 137).
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
Extra carry: 1 1 1 0 1 0 0 0 Carry: 0 1 1 0 1 0 0 0 First addend: 1 0 1 0 1 0 1 Second addend: 1 1 1 0 1 0 0 + -------------------------- Answer: 1 1 0 0 1 1 0 0 1
A full adder circuit can be designed to add numbers in negabinary. The following logic is used to calculate the sum and carries:
Incrementing a negabinary number can be done by using the following formula:
(The operations in this formula are to be interpreted as operations on regular binary numbers. For example, is a binary left shift by one bit.)
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 (1 â 8 â 32 + 64 = 25) minus 1110100 (4 + 16 â 32 + 64 = 52),
Carry: 0 1 âÂÂ1 1 0 0 0 First number: 1 1 0 1 0 0 1 Second number: âÂÂ1 âÂÂ1 âÂÂ1 0 âÂÂ1 0 0 + -------------------- Number: 0 1 âÂÂ2 2 âÂÂ1 0 1 Bit (result): 0 1 0 0 1 0 1 Carry: 0 0 1 âÂÂ1 1 0 0
so the result is 100101 (1 + 4 âÂÂ32 = âÂÂ27).
Unary negation, , can be computed as binary subtraction from zero, .
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.
First number: 1 1 1 0 1 1 0 Second number: 1 0 1 1 0 1 1 ÃÂ ------------------------------------- 1 1 1 0 1 1 0 1 1 1 0 1 1 0
1 1 1 0 1 1 0 1 1 1 0 1 1 0
1 1 1 0 1 1 0 + ------------------------------------- Carry: 0 âÂÂ1 0 âÂÂ1 âÂÂ1 âÂÂ1 âÂÂ1 âÂÂ1 0 âÂÂ1 0 0 Number: 1 0 2 1 2 2 2 3 2 0 2 1 0 Bit (result): 1 0 0 1 0 0 0 1 0 0 0 1 0 Carry: 0 âÂÂ1 0 âÂÂ1 âÂÂ1 âÂÂ1 âÂÂ1 âÂÂ1 0 âÂÂ1 0 0
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.
It is possible to compare negabinary numbers by slightly adjusting a normal unsigned binary comparator. When comparing the numbers and , invert each odd positioned bit of both numbers. After this, compare and using a standard unsigned comparator.
Base 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.
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 with the biggest digit and
we have
So every number with a terminating fraction added has two distinct representations.
For example, in negaternary, i.e. and , there is
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
with
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.