Re: OOT: negative numbers in any base
Re: OOT: negative numbers in any base
- Subject: Re: OOT: negative numbers in any base
- From: Nathan Day <email@hidden>
- Date: Sat, 10 Aug 2002 01:44:04 +0930
Not sure if this is what you are after but
-x = ~(x-1)
for example with an 8 bit -2
2 = b00000010
(2-1) = b00000001
~(2-1) = b11111110
alternatively
~(-x)+1 = x
-2 = b11111110
~(-2) = b00000001
~(-2)+1 = b00000010
it has the advantage that addition and subtraction can be done the same
way
5 - 2 = b00000101 + b11111110 = 5 + 254 = 259 = b100000011
but we only have eight bit so it's b00000011 = 3
On Thursday, August 8, 2002, at 03:54 AM, Michhle Garoche wrote:
Hello,
Sorry for OOT.
Could someone tell me where I can find how the compiler build a
negative number? Some place on the web as I have no access to
specialized libraries, general (not only for binary numbers: I know how
to do it), and no too complicated as my math studies are far behind
me:-)
A simple case: say I enter 1 in base 10, then I negate the number, the
result displayed is -1 as the compiler knows how to write it. Now I
switch to base 3 (not balanced ternary), how can I build the result (I
know how I can build the result for positive numbers, and I know the
limits).
I thought of that way to do it, but not sure if it's the right one:
1 - store the digits in an int variable - done
2 - translate the number into binary string - done
3 - translate the binary string into decimal - to do
4 - translate the decimal into x base string - done
Nathan Day
http://homepage.mac.com/nathan_day/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.