Rahul Sharma (Editor)

Negative base

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Negative base

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).

Contents

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), and negaternary (base −3) to ternary (base 3).

Example

Consider what is meant by the representation 12,243 in the negadecimal system, whose base b is −10:

Since 10,000 + (−2,000) + 200 + (−40) + 3 = 8,163, the representation 12,243−10 in negadecimal notation is equivalent to 8,16310 in decimal notation, while −8,16310 in decimal would be written 9,977−10 in negadecimal.

History

Negative numerical bases were first considered by Vittorio Grünwald in his work Giornale di Matematiche di Battaglini, published in 1885. Grünwald gave algorithms for performing addition, subtraction, multiplication, division, root extraction, divisibility tests, and radix conversion. Negative bases were later independently rediscovered by A. J. Kempner in 1936 and Zdzisław Pawlak and A. Wakulicz in 1959.

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.

Notation and use

Denoting the base as −r, every integer a can be written uniquely as

a = i = 0 n d i ( r ) i

where each digit dk is an integer from 0 to r - 1 and the leading digit dn is > 0 (unless n=0). The base −r expansion of a is then given by the string dndn-1d1d0.

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.

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:

Note that 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 of 0 , 1 , , r 1 , and concatenating those remainders, starting with the last. Note that if a / b = c, 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. This is exemplified in the fourth line of the following example wherein –5 ÷ –3 must be chosen to equal 2 remainder 1 instead of 1 remainder –2.

For example, to convert 146 in decimal to negaternary:

146   /   3 = 48 ,   remainder   2 48   /   3 = 16 ,   remainder   0 16   /   3 = 5 ,   remainder   1 5   /   3 = 2 ,   remainder   1 2   /   3 = 0 ,   remainder   2

Reading the remainders backward we obtain the negaternary representation of 14610: 21102–3.

Proof: (((2·(–3)+1)·(–3)+1)·(–3)+0)·(–3)+2 = 14610.

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 + dr + 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 (shown below in the Python programming language):

Negabinary C# implementation:

C# implementation:

Common Lisp implementation:

Visual Basic implementation:

To negative base

PHP implementation. The conversion from integer to some negative base:

Visual Basic implementation:

To negabinary

The conversion to negabinary (base −2; digits { 0 , 1 } ) allows a remarkable shortcut (C implementation):

Due to D. Librik (Szudzik). The bitwise XOR portion is originally due to Schroeppel (1972).

JavaScript port for the same shortcut calculation:

To negaquaternary

The conversion to negaquaternary (base −4; digits { 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:

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),

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−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

Extra carry: 1 1 0 1 0 0 0 Carry: 1 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

Incrementing negabinary numbers

Incrementing a negabinary number can be done by using the following formula:

2 x ( ( 2 x x ) + 1 )

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),

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−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.

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.

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-integral 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 t := r 1 = b 1 the biggest digit and

T := 0. 01 ¯ b = i = 1 b 2 i = 1 b 2 1 = 1 r 2 1

we have

0. 0 t ¯ b = t T = r 1 r 2 1 = 1 r + 1     as well as 1. t 0 ¯ b = 1 + t b T = ( r 2 1 ) + ( r 1 ) b r 2 1 = 1 r + 1 .

So every number 1 r + 1 + z with a terminating fraction z Z r Z added has two distinct representations.

For example, in negaternary, i.e. b = 3 and t = 2 , there is

1. 02 ¯ ( 3 ) = 5 4 = 2. 20 ¯ ( 3 ) .

Such non-unique representations can be found by considering the largest and smallest possible representations with integral parts 0 and 1 respectively, and then noting that they are equal. (Indeed, this works with any integral-base system.) The rationals thus non-uniquely expressible are those of form

z ( r + 1 ) + 1 b i ( r + 1 )

with z , i 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.

Imaginary-base arithmetic is not much different from negative-base arithmetic, since an imaginary-base number may be considered as the interleave of its real and imaginary parts; using INTERCAL-72 notation,

x(2i) + (2i)y(2i) = x(2i) ¢ y(2i).

References

Negative base Wikipedia