four bit 2's complement notation?

Discussion in 'Homework Help' started by lusito92, Jan 29, 2013.

1. lusito92 Thread Starter New Member

Jan 29, 2013
25
0
Hello,
Im trying to do -4 as a four bit 2's complement notation

So far I have
8 4 2 1
0 1 0 0
1 0 1 1 <-- flip over
<-- Do I have to add +1 to the ones in flip over Im not sure ! Im lost can somebody help me out thanks!

2. thatoneguy AAC Fanatic!

Feb 19, 2009
6,349
731
Work out a truth table for 0-16 and then invert it.

Where is the value zero in that range with only the inversion?

3. lusito92 Thread Starter New Member

Jan 29, 2013
25
0
Sorry? I dont know how to do it like that . I just know how to do like
8 4 2 1
0 1 0 0 <-- this how I should get the binary if is 4 but if is negative what can I do ??

Oct 2, 2009
18,175
5,707

5. lusito92 Thread Starter New Member

Jan 29, 2013
25
0
OK IM really lost am I right or what? why somebody else is telling me about something that I never hear with my teacher about truth table and now you are telling me my answer is yes which answeR?? IM LOST!

If I need to add 1 should be like

1 0 1 1

6. StayatHomeElectronics AAC Fanatic!

Sep 25, 2008
1,020
71
For 2s complement of a negative number you flip all of the bits and add 1.

For -4, as you suggested, you start with +4, 0100, flip all of the bits, 1011, and add 1, 1100.

7. WBahn Moderator

Mar 31, 2012
23,856
7,381
There are a number of ways to keep this straight and to check your results.

First, the most significant bit in two's complement is frequently called "the sign bit" but it really isn't. Yes, it tells you if the number if positive or negative, but it does more than that. It has a weight of -2^(N-1) where N is the number of bits.

So, for 4-bit unsigned integers, the weights are

8 4 2 1

For 4-bit 2's complement integers, the weights are

-8 4 2 1

Since you are representing x = -4 and you know it is a negative number, you know the pattern has to be

-8 4 2 1
1 ? ? ?

So x = -8 + (a three bit unsigned integer) = -4

Solving, this gives us:

(a three bit unsigned integer) = 8 - 4 = 4 (100b)

So the result is 1100.

Another way is to note that x - x = 0. Not a very sexy result, but true none-the-less. However, in a fixed 4-bit representation, 0000 is the same as 10000 because anything to the left of the 4th bit is lost. Mathematically, this is a consequence of working in a "modulo 2^N world".

As a result, we have, in binary:

x - x = 0000 = 10000

-x = 10000 - x

You could do the math in binary, but a shortcut is to note that 10000=1111+1

-x = (1111 + 1) - x

-x = (1111 - x) + 1

The term in parentheses is trivial to compute because we are guaranteed never to need a borrow. For each bit, if we subtract a 0 from a 1 we get a 1 and if we subtract a 1 from a 1 we get a 0. In other words, the result is the bitwise negation of the value being subtracted, which I'll denote by ~x.

-x = ~x + 1

This is, fundamentally, where the algorithm "flip all the bits then add one to the result" comes from. Note that we never said whether x was positive or negative. It doesn't matter! Lot's of people get turned around trying to remember if they have to subtract one if they are starting with a negative number or perhaps they have to add (or is it subtract) the 1 before they flip the bits. The answer is that, regardless of whether x is positive or negative, you obtain -x by flipping the bits and then adding 1.

Finally, you can use part of the preceding development, starting with

-x = 10000 - x

And write this as

-x = 2^N - x

In this case, with N-4 and x=4, the representation for -x is the same as

-x = 2^4 - 4 = 16 - 4 = 12 (1100b)

Hopefully that helps tie some of these things together for you.

8. DerStrom8 Well-Known Member

Feb 20, 2011
2,373
1,353
think you still need a sign bit. The value of -4 is 1100, but I'm thinking you need a 1 at the beginning to show that it's negative 4 instead of 12?

1 1100?

Sorry, it's been a while for me

Matt

9. tshuck Well-Known Member

Oct 18, 2012
3,527
679
Not as long as you are using a 4-bit number. There's no difference in a 4-bit unsigned number versus a 4-bit signed number, except in ways of interpretation.

1100 = -4 = 12(unsigned)

DerStrom8 likes this.