Number Systems Chapter 3 n Binary n Sign
Number Systems Chapter 3 n Binary n Sign Magnitude Binary n 2 s Complement Binary n Hexadecimal n All used for different purposes within a CPU
Binary n All computer processing is carried out digitally. n Processor handles instructions as binary codes – zeros and ones. n All data on a device is 0’s and 1’s.
Converting binary into positive denary integers n Whole positive denary (base ten) numbers are converted into binary as follows: n 135 from denary into binary 128 + 4 + 2 + 1 = 135 MSB 128 64 32 16 1 0 0 0 LSB 8 0 4 1 2 1 1 1
The repeated division method A method for converting denary to binary: 98 in denary into binary: 98 divide by 2 = 49 remainder 0 49 divide by 2 = 24 remainder 1 24 divide by 2 = 12 remainder 0 12 divide by 2 = 6 remainder 0 6 divide by 2 = 3 remainder 0 3 divide by 2 = 1 remainder 1 1 divide by 2 = 0 remainder 1 0 divide by 2 = 0 remainder 0 DIV MOD Read the binary code from the remainder from bottom to the top: 01100010 which equals 98
Hex – Base 16 n Only uses single digits n A = 10 n B = 11 n C = 12 n D = 13 n E = 14 n F = 15 256 16 1
Hex – Base 16 n 75 in denary n is 4 B in Hex 256 16 1 0 4 B 0 64 11 n 0*0=0 n 4*16=64 n 1*11=11 (B) n 64+11=75
Hex Number n 172 in denary n is AC in Hex 256 16 0 0 A 1 C 160 12 n 0*0=0 n 10*16=160 (A) n 1*12=12 (C) n 160+12=172
Convert a Byte to Hexadecimal 1 0 1 n Split the binary in half n n n 1100 = 4+8=12 (C) 1010 = 2+8=10 (A) AC = Hex value 1 0 0
Convert a Hexadecimal to a Byte n The Hexadecimal number 2 E n 1 nibble per hexadecimal column 0 0 1 n 0010 = 2 n 1110 = 14 (E) 0 1 1 1 0
Storing Negative Integers n 1 method is Sign/Magnitude n 75 n -75 MSB 128 +/- 64 32 16 1 0 0 8 4 2 1 1 0 1 1 1 is a Negative, 0 is a Positive
Sign/Magnitude n This method has some limitations n 2 types of data in the same value (MSB is a sign) n Makes calculations difficult by losing 1 bit 127 maximum number +/- 64 32 16 0 Sign 1 0 0 8 4 2 1 1 0 1 1 Value or Magnitude
Storing Negative Integers n Another method is 2 s Complement n -75 128 64 32 16 -128 1 0 1 1 8 4 2 1 0 1 n-128+32+16+4+1=-75
2 s Complement Conversion n -117 n Stage 1 : work out 117 in binary 128 64 32 16 8 4 2 1 0 1 1 1 0 1 n Stage 2 : Reverse the 0’s and 1’s -128 64 1 0 32 0 16 0 n Stage 3 : Plus 1 8 1 4 0 2 1 1 10
Binary Addition Binary Subtraction
Binary Addition Sums n In binary addition there are 5 possible sums: § 0+0=0 § 0+1=1 § 1+0=1 § 1 + 1 = 0, carry 1 § 1 + 1 = 1, carry 1
Binary Addition Example 1 n 75 + 14 = 89 128 64 32 16 8 4 2 1 75= 0 1 0 1 1 14= 0 0 1 1 1 0 89 0 1 1 0 0 1 1 Carry
Binary Addition Example 2 n 79 + 57 = 136 128 64 32 16 8 4 2 1 79= 0 1 0 0 1 1 57= 0 0 1 1 1 0 0 1 136 1 0 0 0 Carry 1 1 1 1
Binary Addition Example 3 n 75 + 75 = 150 128 64 32 16 8 4 2 1 75= 0 1 0 1 1 150 1 0 1 1 0 Carry 1 1
Binary Addition Example 4 n 215 + 138 = 353 (101100001) n (exercise 3 c Page 97) 128 64 32 16 8 4 2 1 215= 1 1 0 1 138= 1 0 0 0 1 0 353 0 1 1 0 0 1 1 1 Overflow 1
Binary Subtraction n n CPUs can’t subtract, they can only add! 75 -14=61 The same as… 75+(-14)=61 Positive + Negative = Negative n To do this we need 2 s Complement -128 64 32 16 8 4 2 1
Binary Subtraction Example 1 n 75 -14=61 n 75+(-14)=61 -128 64 32 16 8 4 2 1 75= 0 1 0 1 1 -14= 1 1 0 0 1 0 61 0 0 1 1 1 1
Binary Subtraction Example 2 n 91 -18=73 n 91+(-18)=73 -128 64 32 16 8 4 2 1 91= 0 1 1 -18= 1 1 1 0 73 0 1 0 0 1 1 1 1
Logical Shifts n A left or right logical shift can be performed on a binary number as a method of multiplication or integer division. n This is the same as an // operation in Python n We can perform multiplications or integer divisions in powers of 2
Left Logical Shifts Example 1 128 64 32 16 8 4 0 1 1 0 n Take this byte – 104 in denary n Shift it left by 1 128 1 64 1 32 0 16 1 8 0 4 0 2 0 1 0 n 11010000 – 208 in denary (104 x 2) n 2**1 is 2
Left Logical Shifts Example 2 128 0 64 1 32 1 16 0 8 1 4 0 2 0 1 0 n Take this byte – 104 in denary n Shift it left by 2 256 128 64 32 16 1 1 0 8 0 4 0 n 110100000 – 416 in denary (104 x 4) n 2**2 is 4 2 0 1 0
Left Logical Shifts Example 3 (activity 5 a page 102) 128 0 64 0 32 1 16 1 8 1 4 0 2 1 1 0 n Take this byte – 58 in denary n Shift it left by 3 - 2**3 (8) 256 128 64 32 16 1 1 1 0 1 8 0 4 0 n 111010000 – 464 in denary (58 x 8) 2 0 1 0
Right Logical Shifts Example 1 128 64 32 16 8 4 1 0 1 1 1 0 n Take this byte – 185 in denary n Shift it right by 1 128 0 64 1 32 0 16 1 8 1 4 1 2 0 1 0 n 01011100 – 92 in denary (185//2) Integer Division n 2**1 is 2
Right Logical Shifts Example 3 Activity 5 b Page 102 128 1 64 0 32 0 16 1 8 1 4 1 2 0 1 1 4 0 2 0 1 1 n Take this byte – 157 in denary n Shift it right by 4 128 0 64 0 32 0 16 0 8 1 n 00001001 – 9 in denary (157//16) Integer Division n 2**4 is 16
Arithmetic Shifts n A left or right arithmetic shift can be performed on a 2 s complement binary number as a method of multiplication or integer division. n Left arithmetic shift – The MSB remains untouched n Right arithmetic shift – use a copy of the MSB as the replacement bits
Left Arithmetic Shifts Example 1 (page 102) -128 64 1 1 32 0 16 1 8 1 4 1 2 0 1 0 n Take this byte -36 in denary n (-128+64+16+8+4) n Shift it 1 place left arithmetic shift -128 64 1 0 32 1 16 1 n 10111000 – -72 (-36 x 2) n -128+32+16+8 8 1 4 0
Right Arithmetic Shifts Example 1 (page 103) -128 64 1 0 32 1 16 1 8 1 4 0 2 0 1 0 2 1 1 0 n Take this byte -72 in denary n (-128+32+16+8) n Shift it 2 places right arithmetic shift -128 64 1 1 32 1 16 0 8 1 4 1 n 1110 – -18 (-72//4) (2**2=4) n -128+64+32+8+4+2
Representing characters n There are two main coding systems that provide conversions of keyboard characters into binary: – ASCII – UNICODE
ASCII n ASCII stands for the American Standard Code for Information Interchange. n It has been adopted as the industry standard way of representing English language keyboard characters as binary codes. n Every keyboard character is given a corresponding binary code. n ASCII uses an 7 -bit code to provide 128 characters.
UNICODE n UNICODE is the new standard to emerge that is replacing ASCII. n Uses 16 bits per character n It is designed to cover more of the characters that are found in languages across the world. n It has become important due to the increased use of the Internet, as more data is being passed around globally.
- Slides: 34