Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

2's complement problem?

This topic is 2783 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

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!