Monday, July 6, 2009

Negative Base Number System


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

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





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-1.....d1d0.


Calculation:

The base r expansion of a number can be found by repeated division by r, recording the non-negative remainders of 0,1,2,.........,r-1; and concatenating those remainders, starting with the last. Note that if a / b = c, remainder d, then bc + d = a. For example, 146 in negaternary:


146 / -3 = -48; reminder = 2
-48 / -3 = 16; reminder = 0
16 / -3 = -5; reminder = 1
-5 / -3 = 2; reminder = 1
2 / -3 = 0; reminder = 2


Therefore, the negaternary expansion of 146 is 21102.

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

The numbers -15 to 15 with their expansions in a number of positive and corresponding negative bases are:

Decimal Negadecimal Binary Negabinary Ternary Negaternary
-15 25 -1111 110001 -120 1220
-14 26 -1110 110110 -112 1221
-13 27 -1101 110111 -111 1222
-12 28 -1100 110100 -110 1210
-11 29 -1011 110101 -102 1211
-10 10 -1010 1010 -101 1212
-9 11 -1001 1011 -100 1200
-8 12 -1000 1000 -22 1201
-7 13 -111 1001 -21 1202
-6 14 -110 1110 -20 20
-5 15 -101 1111 -12 21
-4 16 -100 1100 -11 22
-3 17 -11 1101 -10 10
-2 18 -10 10 -2 11
-1 19 -1 11 -1 12
0 0 0 0 0 0
1 1 1 1 1 1
2 2 10 110 2 2
3 3 11 111 10 120
4 4 100 100 11 121
5 5 101 101 12 122
6 6 110 11010 20 110
7 7 111 11011 21 111
8 8 1000 11000 22 112
9 9 1001 11001 100 100
10 190 1010 11110 101 101
11 191 1011 11111 102 102
12 192 1100 11100 110 220
13 193 1101 11101 111 221
14 194 1110 10010 112 222
15 195 1111 10011 120 210

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.

Program for calculating Negabinary

In python:


def negabinary(i):
digits = []
while i != 0:
i, remainder = divmod(i, -2)
if remainder < 0:
i, remainder = i + 1, remainder + 2
digits.insert(0, str(remainder))
return ''.join(digits)


In C++:

string negabinary(int i)
{
int reminder;
string digits;
while(i != 0)
{
reminder = i % -2;
i /= -2;
if(reminder < 0)
{
i++;
reminder += 2;
}
digits.push_back(reminder+'0');
}
reverse(digits.begin(),digits.end());
return digits;
}


The same procedure it to be followed for calculating other negative number system.

Source: Wikipedia


6 comments:

  1. why you increment "i" when "reminder < 0"

    ReplyDelete
  2. Does it have any application specifically in the field of digital logic designing...or its just concept???

    ReplyDelete
    Replies
    1. I have been studying reversible logic design for a few days at univ, and studied digital logic design in previous years, but no, haven't seen any application yet. I think its just a fancy concept. But I only know very little, so you need to do more research.

      Delete
  3. -5 / -3 = 2; reminder = 1?

    No, -5 / -3 = 1; reminder = 2

    ReplyDelete
    Replies
    1. Note that if a / b = c, remainder d, then bc + d = a.

      Delete