F
Frederick Gotham
I have a general idea about how negative number systems work, but I'd
appreciate some clarification if anyone would be willing to help me.
Let's assume we're working with an 8-Bit signed integer, and that it
contains no padding.
Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).
The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
unsigned integer would represent the number.
But if the number is negative, then there's separate methods to correlate
the negative bit-pattern with its corresponding positive value:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.
It seems we get the following ranges for each format: (Are they right?)
(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
(3) Two's complement: -128 through +127
It would seem that each number system has the following advantages:
(1) Sign-magnitude:
Efficient conversion from negative to positive.
(2) One's complement:
There's only one bit-pattern for zero.
One extra unit in the negative range.
(3) Two's complement:
There's only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Are there any other advantage/disadvantages to be aware of?
The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.
If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:
var1 == 0000 0000
var2 == 1000 0000
How does the machine handle comparison of these two variables? Would it
interpret:
if ( var1 == var2 )
as:
if ( !(var1 & 127 || var2 & 127) )
I'd ask more question as people reply...
appreciate some clarification if anyone would be willing to help me.
Let's assume we're working with an 8-Bit signed integer, and that it
contains no padding.
Firstly, I realise that the MSB is known as the sign-bit, and that it
indicates whether the number is positive or negative (irrespective of
which negative number system is used).
The next seven bits tell us the value. If the number is positive, then
the seven bits represent the number in the same domestic way in which an
unsigned integer would represent the number.
But if the number is negative, then there's separate methods to correlate
the negative bit-pattern with its corresponding positive value:
(1) Sign-magnitude: The bit-pattern is exactly the same.
(2) One's complement: Toggle each bit.
(3) Two's complement: Toggle each bit, then add 1.
It seems we get the following ranges for each format: (Are they right?)
(1) Sign-magnitude: -127 through +127
(2) One's complement: -128 through +127
(3) Two's complement: -128 through +127
It would seem that each number system has the following advantages:
(1) Sign-magnitude:
Efficient conversion from negative to positive.
(2) One's complement:
There's only one bit-pattern for zero.
One extra unit in the negative range.
(3) Two's complement:
There's only one bit-pattern for zero.
One extra unit in the negative range.
Addition with negative numbers can be performed exactly as if
they were positive.
Are there any other advantage/disadvantages to be aware of?
The next thing I want to discuss is the whole idea of having more than
one bit-pattern for a specific value... zero in particular!.
If we have a machine that uses sign-magnitude, and we have two separate
variables that hold the value zero, is it possible for them to have
different bit patterns? i.e.:
var1 == 0000 0000
var2 == 1000 0000
How does the machine handle comparison of these two variables? Would it
interpret:
if ( var1 == var2 )
as:
if ( !(var1 & 127 || var2 & 127) )
I'd ask more question as people reply...