Home

Binary Quiz to try

Number Exercise 1

Number Exercise 2

This week we look at 2's compliment and how a program is executed by a computer by simulating the Little Man in the Computer (LMC).

2's Compliment and Binary Addition

How to perform subtraction by adding two numbers.

	
Example:   10 - 5 = 5

That is the same as 10 + (-5) = 5

All we need to do is use negative numbers - This is where 2's complement comes in. It is a software method of converting a positive binary number to its negative equivalent. The benefit is a CPU can then perform binary math and produce a result which subtracts. The drawback is it requires more bits, therefore memory, as the same amount of bits must now include the negative equivalents. Note, it is the software that is aware it is using 2's compliment, not the processor!

To convert a positive binary integer to its negative equivalent you use this rule:

From the right, or LSB, keep all bits up to and including the first 1, then flip the rest, i.e. make 1's - 0's and 0's -1's

Example:  Binary for 5 Base 10 is 0101
                To find the 2's complement, or negative equivalent.
                Keep up to and including the first one.       0101
                Now flip the rest                                       1011
Therefore the 2's complement of 0101 is 1011.  Because 0101 = 5, then 1011 = (-5)

Let's test this on our subtraction above:

Some key points.

  1. In 2's complement if a 1 is the MSB this always means the number will be negative.
  2. If the MSB is 1 after adding then you MUST convert using 2's complement to find the correct -ve value.

In 4 bit binary the range of values is between 0 and 15. In 4 bit 2's complement the range is between -8 and +7 (note 8 and 7 add to make 15 and that the larger value is the negative value. You can use this rule to find the range for any number of bits).

Example: In 8 Bit binary the range is 0 to 255. In 8 bit 2's complement the range is -128 to +127. (128+127=255)

Now try the quiz.

Same values represented in the two different forms in the table below.

Binary Signed (2's Complement) Unsigned (non 2's Complement) 

--------------------------------------------------------------------------------
 
00000001              1                          1 
11111111             -1                        255 
01111111            127                        127 
10000001           -127                        129 
10000000           -128                        128 

--------------------------------------------------------------------------------
 

Little Man Computer (CPU Instruction Cycle simulation)

The Little Man Computer simulation is used to demonstrate program flow and how data is shifted between memory and the CPU. you will also see how programs 'flow' and 'branch' depending on test conditions.

Further Reading



LMC step by step PDF

LMC step by step guide

MS Access' use of Binary 2's Compliment

Number Exercise 1 SOLUTIONS

Number Exercise 2 SOLUTIONS