Sign in to follow this  
nuclear123

2's complement problem?

Recommended Posts

nuclear123    119
i was recently told to do this

use only 4 bits and add +9 , -4 using 2's (these numbers are represented in 2's complement)?
is this even possible?
wouldn't u need 5 bits atleast??

Share this post


Link to post
Share on other sites
Washu    7829
Quote:
use only 4 bits and add +9 , -4 using 2's

Would help a bit if you clarified exactly what the heck you're saying.

Depending on if you're using signed or unsigned, 4 bits may be enough. Unsigned gives you between [0,15], signed gives you [-8,7]

Share this post


Link to post
Share on other sites
Nypyren    12061
Adding and subtracting will overflow with 4 bits, but the overflow is...nondestructive? in this case, and you'll get the correct result.

2's complement form is a way to convert a number to/from a bitwise representation.

For example if I give you the following bits without any information about what form they're in, they're just bits:

1001

That could be 9 in unsigned form or -7 in 2's complement form.

Addition/subtraction operations don't care about the form the bits are in.


-4 in 2's complement 4-bit form is 1100.



1001
+ 1100
------
0101 with carry


so the result is that you have 5 as the result, and your carry flag has been set.

Share this post


Link to post
Share on other sites
alvaro    21246
Using 4 bits and 2's complement you can only represent integers in the range [-8,8), so your question about adding +9 and -4 doesn't make much sense.

In a more mathematically-sound way of looking at things, 4-bit numbers are actually congruency classes of the integers modulo 16. In that sense, +9 and -7 are the same number (1001), and so are -4 and +12 (1100). You can add them together and the result is 9+12 = 21 = 5 (0101).

Share this post


Link to post
Share on other sites

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