# Decimal to negabinary conversion

I found in Wikipedia that you can convert base 10 to a negative base simple by dividing to a base and keeping remainder like this: Now I'm stuck here.

I want to convert two digits -6 and 6 no base(-2)

Now example:

I do this:

6 / -2 = -3, remainder 1
-3/ -2 = 1, remainder 1
1/ -2 = 0, remainder 0

so, the final result is 0011 which is not quite right, because 1x(-2) + 1 = -1, not six.

HELP!

Update:

And one more question concerning negabinary:

Suppose we use 2n bits to represent integers using base −2 encoding, for n > 0. What is the largest integer we can represent? What is the smallest?

What I'm thinking is that the smallest number is 0, because we cannot represent -1 in negative numbers of bits. 2x(-1) = -2. ANd there's no limit to a max number since 2x(10000) will fit in 200000 bytes.

6/-2=-3 with remainder 0, not 1

-3/-2=2 with remainder 1 (remember that your remainders have to be positive, like the -5/-3 in the example)

2/-2=-1 with remainder 0

-1/-2=1 with remainder 1

1/-2=0 with remainder 1

giving $11010_{-2}=6_{10}$

For your question about largest and smallest numbers representable in $2n$ bits, the largest positive number has all the positive bits set, so you have $1+4+16+\ldots$ This is $\sum_{i=0}^{n-1}4^i=\frac{4^n-1}{3}$. The smallest number has all the negative bits set and is twice this large in absolute value, so it is $-\frac{2(4^n-1)}{3}$

• Oh, right. I should round them. -3/-2 = 1.5 => 2. Great. Any suggestion on the second question? – user194076 Sep 11 '11 at 20:40
• It is not rounding, it is the ceiling function for negative bases, floor function for positive bases. That makes sure the remainder is positive. – Ross Millikan Sep 11 '11 at 20:49
• This works great. The only question I have is the last question. I do not quite understand why negative bits set is twice this large in absolute value. Can you please explain this to me a little more? I would appreciate that. – user194076 Sep 13 '11 at 4:05
• @user194076: the place values are 1, -2, 4, -8, ... So each negative value place is twice the previous positive value place in absolute value. As you specified 2n bits, we will end with a negative value place. And we don't need a sign bit. – Ross Millikan Sep 13 '11 at 4:20
• @user194076: The sum is a geometric series as each term is a fixed multiple (here 4) of the preceding. The formula for its sum is given in en.wikipedia.org/wiki/Geometric_series – Ross Millikan Sep 13 '11 at 4:33