Hi,

Although i'm 31 years old, im following a 3 year programming cours in school (in the evening) to learn new programming languages like C++, VB and JAVA.
We learnt about the 2-complement notation (in binary). When someone asked what the use is for this notation the teacher didn't know.
So my question.
Is 2-complement still used ?
Are there special asm commands for ?

thanks for reading.
Posted on 2003-10-27 17:17:58 by Jurgen
Of the current computer architectures (from embedded micros to mainframes) I have looked at, 2's complement is used for integers and signed-magnitude for floating-point. Some architectures can also directly support BCD.

I suppose you aren't interested in obsolete formats...
Posted on 2003-10-27 17:44:00 by tenkey
neg - two's complement

Here is some quick info on the two's complement:
Goto the section "Twos Complement Notation".
http://stud.fh-wedel.de/~ia5023/OFFENT/asm/tut/Lecture%2001.htm

Short answer to the question if it's used, yes, sometimes neg is used for different reasons. (can't give any examples now)
Posted on 2003-10-27 18:21:39 by scientica
Twos compliment is the mainstay in asm!

1's compliment is insuficient since it has the posibility of have TWO values for zero "1111" or "0000" since the MSB is the sign bit. (Example is a nibble (4 bits)).

For a nibble in 1s, your range would be:

0000 = +0
0001 = +1
0010 = +2
0011 = +3
0100 = +4
0101 = +5
0110 = +6
0111 = +7

1111 = -0
1110 = -1
1101 = -2
1100 = -3
1011 = -4
1010 = -5
1001 = -6
1000 = -7

If you look at this, there is 15 unique numbers in 16 registers allocations. This is why 2's came to be! Engineers saw the waste and said "Why cant we have 8 positive and 8 negative!". As such, zero is considered positive and the 2's compliment is born:

0000 = +0
0001 = +1
0010 = +2
0011 = +3
0100 = +4
0101 = +5
0110 = +6
0111 = +7

1111 = -1
1110 = -2
1101 = -3
1100 = -4
1011 = -5
1010 = -6
1001 = -7
1000 = -8

Its 2's compliment that forces the HLL numberical limits on things like integers (SIGNED WORDS ~ 16 bits). 2^15 = 32768! This means you get 32768 unique values in both the positive and negative directions. But 0 is considered positive, so the actual range is: 0 -> 23767 (+'s) and -1 -> -32768 (-'s). Check your favour HLL and you will see the same... this is just how they got their values for its constraint. This was taken from VB "Integer 2 bytes -32,768 to 32,767"!

Another bonus, is two's compliment allows for simple addition in binary as well and you use it all the time."ADD AL, BL" is just this!. BL = -3, AL = 7:

BL = 11111101b
AL = 00000111b
-- --------------------
AL = 00000100b

(we ignor the carry)


When you do a conditional in MASM "IF SDWORD PTR EAX < -2" your doing a two's compiment evaluation. Its used everywhere really. Your teachers answer would be better suited for 1's compliment than 2's! 1's compliment is not really suited for math but more logic such as AND/OR/XOR/NET etc.

(PS: I'd also be questioning the merrit of your teachers ability to teach the course. Sounds like he/she is reading a book out loud more than their teaching...)
:alright:
:NaN:
Posted on 2003-10-27 19:05:22 by NaN