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