[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
Subject: Ones Complement versus Twos Complement - RE: Use CK_UNAVAILABLE_INFORMATION
Stef had a question: Is it ever the case that ~0UL ~= (U_LONG)-1;The answer is when the processor is not using twos complement arithmetic for negative numbers.
In computer architecture text books there are a few ways of handling negative numbers.
1) Sign bit: the number has a sign bit, so -1 is <Signbit=1>|| 1 or 0x80000001 if the word size is 32 bit. In this mode, the processor looks at the sign bit of the operands and decides which unit (addr or subractor) to send the two operands. Multiplication and division strips the sign bits, and calculates the sign separately.
2) One's complement: negative numbers are the ones complement of the absolute value (-1 = ~1). In this mode, the processor only has an addr. If the result overflows, the carry bit is added back to the result. Note: in ones complement, there are two zeros: 0 and ~0 (or -0).
3) Twos complement: negative numbers are the twos complement of the absolute value. Twos complement can be thought of as any of the following:
-1 = (~1+1) -1 = 2^wordsize - 1In twos complement, you don't need to do anything special for negative numbers, you can just use the same addr and multiplier and everything works out OK.
In my 30 years I've seen 1) in real life (out side of text books) in only 2 places: Sign bits on floating point numbers, and sign bits in software bit numb libraries. I've seen 2) outside of texts books in one place: the exponent field of floating point numbers on the CDC Cyber. I've hear rumors that the old Cray computers also used ones complement.
I believe occurrence of ones complement for any fixed point numbers on a real processor that actually has a C compilier would be exceedingly rare.
Description: S/MIME Cryptographic Signature