Sign in to follow this  

2's complement question

This topic is 2540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2540 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this