Binary

Discussion in 'Programmer's Corner' started by poseidon, Oct 13, 2008.

  1. poseidon

    Thread Starter New Member

    Oct 13, 2008
    3
    0
  2. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    It is twos compliment without the sign-bit explicitly specified: so in twos compliments -5 is 1011 - Understand why? The MSB '1' is the specified negative bit - so see it as (1)011 where (1) means negative and 011 means 5. If you read the article in context it is merely describing the idea of bit-flipping and adding one for going from 5 (101) to -5 (011) without the complication (as if there is any) of the sign-bit; naturally the bit-flipping and adding one works with the sign-bit too.

    Dave
     
  3. poseidon

    Thread Starter New Member

    Oct 13, 2008
    3
    0
    011 is 3 though? 1011 is negative 3. How do you make -5 - sorry still can;t see it.
     
  4. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    In unsigned binary, yes.

    No, 1011 in twos compliment is -5. Would you like an explanation as to why?

    To understand how 011 becomes -5 you need to look at the following section from the e-book:

    Code ( (Unknown Language)):
    1. zero             0000
    2. positive one     0001          negative one     1111
    3. positive two     0010          negative two     1110
    4. positive three   0011          negative three   1101
    5. positive four    0100          negative four    1100
    6. positive five    0101          negative five    1011
    7. positive six     0110          negative six     1010
    8. positive seven   0111          negative seven   1001
    9. .                              negative eight   1000
    10.  
    It then goes on to say:

    So do the bit-flip and add one to the twos compliment, and then apply the appropriate sign-bit, where '0' = positive and '1' = negative.

    Actually, I think the explanation is clumsy, even if correct. The idea is that the LSBs are the twos compliment and therefore should be considered as the part that the flip and add operation is applied to - the sign-bit is something you effectively add afterwards depending on whether you are switch from positive-negative or negative-positive.

    Dave
     
  5. poseidon

    Thread Starter New Member

    Oct 13, 2008
    3
    0
    Thanks Dave

    I think for me the easiest way to keep this in my head is to see the MSB, the 1, as the value which the sum of the other bits (which can total a maximum of 1 less than the value in the MSB), take away from themselves to give a necessarily negative result. This is "excess notation", I believe, is it not?
     
  6. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    The way to think about this is to look at the value of each bit placing for twos compliment; consider 4-bits xxxx, from the left:

    1st x: equals -8 (The MSB which in twos compliment is the sign-bit always considered from a "binary" perspective as minus the value)

    2nd x: equals 4

    3rd x: equals 2

    4th x: equals 1

    Note binary progression of bits 1-2-4-8-16-32-64-128-256-512-1024-2048...

    So when we have 1011 (the e-book tells you -5 ;)), we get:

    (1x-8) + (0x4) + (1x2) + (1x1) = -5

    You may notice that in twos compliment all-1s, i.e. 1111.., is always -1 - can you see why?

    I don't think you are getting it wrong, it is just a case of clarifying it in your mind, you're not too far off getting it.

    Dave
     
  7. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Two's complement is pretty counterintuitive, but what it has going for it is that if you add 5 and -5 you get 0, exclusive of an overflow in a bit higher than you care about. Dave might have mentioned this, but if he did I missed it in my quick glance.

    Let's look at the two's complement representations of 5 and -5, and their sum, using 4-bit binary numbers:
    Code ( (Unknown Language)):
    1.  
    2. Decimal    Binary
    3.  5           0101
    4. -5           1011
    5. ---         -----
    6.  0        (1)0000
    7.  
    In the addition on the right, going from right to left the way addition is normally performed, 1 + 1 = 10, so write 0 and carry the 1. The next addition is 0 + 1 + the carry = 10, so write 0 and carry the 1 again. Next is 1 + 0 + carry = 10, so write 0 and carry the 1. The final addition is 0 + 1 + carry = 10, so write 0 and carry the 1, which is shown in parentheses, and is at a higher bit position than is allowed in 4-bit binary numbers.

    Hope that helps.
    Mark
     
  8. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    I didn't mention that Mark, so thanks for adding your part.

    If you think about it though, the overflow in twos compliment beyond the sign-bit is meaningless. The technique gets around this by implicitly handling the overflow: 5 + -5 = 0000 - without the overflow, which intuitively is 0 in decimal. If you add the binary value out with the bit-placement value it still works: (0x-8) + (0x4) + (0x2) + (0x1) = 0

    Dave
     
Loading...