15
Programming Concepts Section 1-1
Example: To treat –19 in decimal as signed binary, 0013 hex (the absolute
value of 19) is subtracted from FFFF hex and then 0001 hex is added to yield
FFED hex.
Complements
Generally the complement of base x refers to a number produced when all
digits of a given number are subtracted from x – 1 and then 1 is added to the
rightmost digit. (Example: The ten’s complement of 7556 is 9999 – 7556 + 1 =
2444.) A complement is used to express a subtraction and other functions as
an addition.
Example: With 8954 – 7556 = 1398, 8954 + (the ten’s complement of 7556) =
8954 + 2444 = 11398. If we ignore the leftmost bit, we get a subtraction result
of 1398.
Two’s Complements
A two’s complement is a base-two complement. Here, we subtract all digits
from 1 (2 – 1 = 1) and add one.
Example: The two’s complement of binary number 1101 is 1111 (F hex) –
1101 (D hex) + 1 (1 hex) = 0011 (3 hex). The following shows this value
expressed in 4-digit hexadecimal.
The two’s complement b hex of a hex is FFFF hex – a hex + 0001 hex = b hex.
To determine the two’s complement b hex of “a hex,” use b hex = 10000 hex –
a hex.
Example: to determine the two’s complement of 3039 hex, use 10000 hex –
3039 hex = CFC7 hex.
Similarly use a hex = 10000 hex – b hex to determine the value a hex from the
two’s complement b hex.
Example: To determine the real value from the two’s complement CFC7 hex
use 10000 hex – CFC7 hex = 3039 hex.
The CP Series has two instructions: NEG(160)(2’S COMPLEMENT) and
NEGL(161) (DOUBLE 2’S COMPLEMENT) that can be used to determine the
two’s complement from the true number or to determine the true number from
the two’s complement.
FFFF
1111 1111 1111
1111
0013
0000 0000 0001 0011
−
)
FFEC
1111 1111 1110
1100
000
1
0000 0000 0000 0001
+)
FFED
1111 1111 1110 1101
True number
Two's complement