Complement

From Compsci1

(Difference between revisions)
 
(One intermediate revision not shown)
Line 1: Line 1:
-
<pre>COMPLEMENT
+
COMPLEMENT
We use complement to help store negative numbers in the computer.  We use + and – to mark positive and negative numbers.  However, computers can only read information in bits, 0s or 1s, so we use a new method to store negative numbers.
We use complement to help store negative numbers in the computer.  We use + and – to mark positive and negative numbers.  However, computers can only read information in bits, 0s or 1s, so we use a new method to store negative numbers.
-
 
+
<pre>
-
The 1’s complement (ones complement) of a binary numbers is found by changing all the 1s to 0s and all the 0s to 1s.
+
The 1’s complement (ones complement) of a binary numbers is found  
 +
by changing all the 1s to 0s and all the 0s to 1s.
1001 0011
1001 0011
1s complement 0110 1100
1s complement 0110 1100
-
The 2’s complement (twos complement) of a binary number is found by getting the ones complement first and then adding 1.
+
The 2’s complement (twos complement) of a binary number is found  
 +
by getting the ones complement first and then adding 1.
EG:
EG:
Line 16: Line 18:
2’s complement 0011 1011
2’s complement 0011 1011
-
Negative numbers are the 2’s complement of the corresponding positive number.  To subtract a number, find the 2’s complement and add it.
+
Negative numbers are the 2’s complement of the corresponding positive number.  
 +
  To subtract a number, find the 2’s complement and add it.
Sign bit:
Sign bit:
The left most bit of a binary number is the sign bit:
The left most bit of a binary number is the sign bit:
0 means the number is positive
0 means the number is positive
-
1 means the number is negative.
+
1 means the number is negative.</pre>
Twos complement overflow flag:
Twos complement overflow flag:
If we add or subtract numbers and part of the answer overflows the available capacity of the storage space this can cause a problem.  If we add 2 negative numbers in twos complement the answer can overflow beyond the capacity and give the impression of a positive number.  To catch this problem the computer has the TWOS COMPLEMENT OVERFLOW FLAG which can be written into a program and checked after an operation to see if this has happened.
If we add or subtract numbers and part of the answer overflows the available capacity of the storage space this can cause a problem.  If we add 2 negative numbers in twos complement the answer can overflow beyond the capacity and give the impression of a positive number.  To catch this problem the computer has the TWOS COMPLEMENT OVERFLOW FLAG which can be written into a program and checked after an operation to see if this has happened.
-
</pre>
 

Current revision as of 20:08, 11 October 2006

COMPLEMENT

We use complement to help store negative numbers in the computer. We use + and – to mark positive and negative numbers. However, computers can only read information in bits, 0s or 1s, so we use a new method to store negative numbers.

The 1’s complement (ones complement) of a binary numbers is found 
by changing all the 1s to 0s and all the 0s to 1s.
			1001 0011
1s complement	0110 1100

The 2’s complement (twos complement) of a binary number is found 
by getting the ones complement first and then adding 1.

EG:
Binary number		1100 0101
1’s complement		0011 1010
					+  1
				_______
2’s complement		0011 1011

Negative numbers are the 2’s complement of the corresponding positive number. 
 To subtract a number, find the 2’s complement and add it.

Sign bit:
The left most bit of a binary number is the sign bit:
0	means the number is positive
1	means the number is negative.

Twos complement overflow flag: If we add or subtract numbers and part of the answer overflows the available capacity of the storage space this can cause a problem. If we add 2 negative numbers in twos complement the answer can overflow beyond the capacity and give the impression of a positive number. To catch this problem the computer has the TWOS COMPLEMENT OVERFLOW FLAG which can be written into a program and checked after an operation to see if this has happened.

Personal tools
Handy Pages