decimal hexadecimal binary 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111Therefore a byte of data can be expressed as two hex digits.
Example: 10001010 Split into two 4-bit fields 1000 1010 = 8A (hexadecimal)
However it makes sense to allocate them in such a way that arithmetic operations remain as simple as possible and do not involve extra decoding and encoding steps.
The most general way of representing negative integers in computing and microprocessing is the "Two's Complement" (TC) notation. Using TC notation, positive and negative numbers may be treated exactly alike during calculations and interpreted as positive or negative only on input/output. Particularly important is the necessity for the sum of a positive and a negative number to yield the correct result. I will show below how the TC of a number is calculated and that the above condition is satisfied.
a) Invert the number (0 becomes 1; 1 becomes 0) b) Add 1
So if 90 (decimal) is 01011010 in binary (5A in hex) then -90 in TC is found as follows:
a) Invert: 10100101 (A5 in hex) b) Add 1: 10100110 (A6 in hex)We must prove that this is correct, which we do by adding 90 to -90:
01011010 90 10100110 -90 -------- --- Carry -> 1 00000000 0 ======== ===The Carry is ignored; it has "spilled" over the end of the register.
What is 'zero' in Two's Complement?
00000000 0 a) 11111111 b) 1 00000000 -0 = 0 as requiredNote that in TC notation, a negative number always has its most significant bit set (equal to 1).
We want to know 25H - 17H (numbers in hex).
Take the TC of 17H: 00010111 = 17 a) 11101000 b) 11101001 = E9 The TC of 17H is E9H. Now add them together: 00100101 = 25 11101001 = -17 Carry -> 1 00001110 = OEH (Correct, try it)What if we wanted 17H - 25H?
Take the TC of 25H: -25H = DB Now add 17H: 11011011 = DB 00010111 = 17 + Carry -> 0 11110010 = F2This is obviously a negative number (MSB is set). To find out which number this is the negative of, we find the TC:
11110010 = F2 a) 00001101 b) 00001110 = 0ESo F2H is -0EH which is what we should expect.
On any of these calculations, the answer may have been outside the range of -128 to +127. In this case, the answer will appear negative when it should be positive and vice-versa. The ALU can detect this condition, known as "overflow" and set its overflow flag accordingly. This flag may then be tested in the program and appropriate action taken.
EXAMPLE 25H + 60H (=85H) 00100101 25H 01100000 +60H -------- ---- Carry -> 0 10000101 85H, but MSB indicates negative no. ======== ==== 0 11000000 <- carrys from previous bit ^ ^The OVERFLOW flag is set when the Carry out of the MSB (into CARRY) is different from the Carry out of the next most significant bit. In other words, it is the exclusive-OR of the carrys arrowed above.