2's complement question

Started by
4 comments, last by fastcall22 13 years, 3 months ago
what does 2's complement mean exactly??

1.) 2's complement states that the numbering system were using will use this form for example
(0001)2 = (+1)10 (in 2's complement form)
(1111)2 = (-1)10 (in 2's complement form)

2.) does 2's complement just represent the negative value of a positive number? for example
(0001)2 = (+1)10 (in 2's unsigned form)<-- not 2's complement
(1111)2 = (-1)10 (in 2's complement form) <-- 2's complement
Advertisement
Quote:Original post by nuclear123
what does 2's complement mean exactly??


It means that the most significant bit is used to determine the number's signedness.

Quote:
1.) 2's complement states that the numbering system were using will use this form for example
(0001)2 = (+1)10 (in 2's complement form)
(1111)2 = (-1)10 (in 2's complement form)


Yes. A helpful trick is to flip the bits and add one when you have a negative number, so:

11112 since the most significant bit is set, we know this number is negative
00002 flip the bits
00012 add 1
Resulting in -110

Another example:
10012 MSB is set, this number is negative
01102 flip the bits
01112 add 1
Resulting in -710

Quote:
2.) does 2's complement just represent the negative value of a positive number? for example
(0001)2 = (+1)10 (in 2's unsigned form)<-- not 2's complement
(1111)2 = (-1)10 (in 2's complement form) <-- 2's complement


No. Remember that 2's complement is a way of interpreting a set of bits. The bits 11112 can be either -1 as a signed integer, or a 15 as an unsigned integer.

Take this inequality:

11112 > 01112 as unsigned integers evaluates to true since 15 > 7. However, as signed integers this evaluates to false since -1 < 7.
Quote:Original post by _fastcall
Quote:Original post by nuclear123
what does 2's complement mean exactly??


It means that the most significant bit is used to determine the number's signedness.
Actually that's a totally inadequate description, as it could fit several signed number representations.

Your best bet is to read these:
http://en.wikipedia.org/wiki/Two's_complement
http://en.wikipedia.org/wiki/Signed_number_representations
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
One of the main reason why 2's compliment exist is their way to efficiently represents negative and positive numbers.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
AFAIK the main reason is the simplicity with which addition and subtraction can be done in hardware. Think on the famous
12 + 12 = 102
and carry bit propagation.
Quote:Private message from nuclear123
how does a computer need to determine wether the first value is negative or the second value?

(0001)2 = (+1)10 (in 2's unsigned form)<-- unsigned
(1111)2 = (-1)10 (in 2's complement form) <-- 2's complement

how does it know that 0001 is unsigned form and 1111 is 2's complement negative, and not 1111 unsigned?


Anyway: The computer doesn't know. A computer doesn't distinguish a type from a set of bits. The type is inferred by the operations performed on those bits. For example, there are two assembler instructions for multiplying two (Intel 8088) 16-bit registers: mul (unsigned integer multiply), and imul (signed integer multiply).

So, in a 4-bit architecture using 2's complement,
00012 will always be interpreted as +110 using mul and imul, and
11112 will be interpreted as +1510 when using mul, but will be interpreted as -110 when using imul.

This topic is closed to new replies.

Advertisement