With 1 coin 2 possibilities: Head / Tail or 0/1 77
What if I have 2 coins?! 0 0 With 2 coins, I can have 4 possibilities 0 1 1 0 1 1 With 3 coins, I can have 2*2*2=8 possibilities With 4 coins, I can have 2 4 =16 possibilities 78
Computer architecture Number Systems 79
Outline Number systems Definitions Positional number systems Decimal system Binary system Hexadecimal system Octal system Comparison of numbers in the four systems Nonpositional number systems 80
Introduction A number system defines how a number can be represented using distinct symbols. A number can be represented differently in different systems. Example: (2A) 16 = (52) 8 = (42) 10 We use characters to create words. The number of symbols is limited. We need to repeat characters and combine then to create words. 81
Introduction It is the same for numbers. There s a limited number of symbols (digits) to represent numbers. Digits need to be repeated. 2 categories of number systems: 1. Positional 2. Non-Positional 82
274 200 + 70 + 4 2 10 2 + 7 10 1 + 4 10 0 83
15.2 10 + 5 + 0.2 1 10 1 + 5 10 0 + 2 10-1 84
Positional number systems In a positional number system, the position a symbol occupies in the number, determines the value it represents. In this system, a number represented as : ± has the value of : =± + + + + + + + + b is the base 85
Decimal system (base 10) We have 10 symbols to represent a number. The set of symbols is S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} ± used to show that a number can be positive or negative. However these signs are not stored in computers (discussed in the next chapter). In the decimal system, a number is written as : ± An integer (number with no fractional part) is familiar to all of us, its value =± 10 + + 10 + 10 + 10 86
Decimal system (base 10) Another way to show an integer is to use place values, which are powers of 10 for decimal numbers. 10 10 10 10 10 Place value ± Number =± 10 + 10 + + 10 + 10 + 10 Value 87
Decimal system (base 10) Example 1 Place value of (+224) 10 in the decimal system 10 10 10 Place value + 2 2 4 Number =+ 2 10 + 2 10 + 4 10 Value 88
Decimal system (base 10) Example 2 Place value of (-7508) 10 in the decimal system 10 10 10 10 Place value 7 5 0 8 Number = (7 10 + 5 10 + 0 10 + 8 10 ) Value = (7 1000 + 5 100 + 0 10 + 8 1) Value 89
Decimal system (base 10) Reals A real (number with fractional part) is also familiar. Represented by ± Its value =± 10 + + 10 + 10 + 10 + 10 + 10 + + 10 90
Decimal system (base 10) Reals - Example 10 10 10 10 Place value + 2 4 1 3 Number =+ (2 10 + 4 1 + 1 0.1 + 3 0.01) Value 91
Binary system (base 2) The word binary is derived from the Latin root bini (or two by two) We use only 2 symbols, S = {0, 1} The symbols in this system are referred to as binary digits or bits. Data and programs are stored in the computer using a string of bits. Why? Because computer is made of electronic switches that can only have 2 states, on and off. We can represent an integer as : ± The value is calculated as =± 2 + + 2 + 2 + 2 92
Binary system (base 2) Another way to show a binary number using place values: 2 2 2 2 2 Place value ± Number =± 2 + 2 + + 2 + 2 + 2 Value 93
Binary system (base 2) Example (11001) 2 in binary is the same as 25 in decimal 2 2 2 2 2 Place value 1 1 0 0 1 Number = 1 2 + 1 2 + 0 2 + 0 2 + 1 2 Value = 16 + 8 + 0 + 0 + 1 Value 94
Binary system (base 2) Maximum value The maximum value of a binary integer with k digits is when all digits equal to 1. n max =2 k -1 Example: if k = 5, n=(11111) 2 = 2 5-1=(31) 10 95
Binary system (base 2) Reals A real (number with fractional part) is also familiar. Represented by ± Its value =± 2 + + 2 + 2 + 2 + 2 + 2 + + 2 96
Binary system (base 2) Reals - Example (101.11) 2 in binary is equal to 5.75 in decimal 2 2 2 2 2 Place value + 1 0 1 1 1 Number =+ 1 4 + 0 2 + 1 1 + 1 2 + 1 2 Value =+ 4+0+1+0.5+0.25=5.75 97
Hexadecimal system (base 16) Binary system is used in computers however it is not convenient for representation of numbers outside the computer. There s an obvious relation between binary and hexadecimal systems (as well as with octal system). We use only 16 symbols, S = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} A, B, C, D, E, Fare equivalent to 10, 11, 12, 13, 14, and 15 respectively. The symbols in this system are referred to as hexadecimal digits. We can represent an integer as : ± The value is calculated as =± 16 + + 16 + 16 + 16 98
Hexadecimal system (base 16) Another way to show an hexadecimal number using place values: 16 16 16 16 16 Place value ± Number =± 16 + 16 + + 16 + 16 + 16 Value 99
Hexadecimal system (base 16) Example (2AE) 16 in hexadecimal is the equivalent to 686 in decimal 16 16 16 Place value 2 A E Number = 2 16 + 10 16 + 14 16 Value = 512 + 160 + 14 Value 100
Hexadecimal system (base 16) Maximum value The maximum value of an hexadecimal integer with k digits is equal to n max =16 k -1 Example: if k = 5, n=16 5-1=(1 048 575) 10 It is not common to represent real numbers in hexadecimal. We leave it as an exercise. 101
Octal system (base 8) Devised to show the equivalent of the binary system outside the computer. We use only 8 symbols, S = {0, 1, 2, 3, 4, 5, 6, 7} The symbols in this system are referred to as octal digits. We can represent an integer as : ± The value is calculated as =± 8 + + 8 + 8 + 8 102
Octal system (base 8) Another way to show an octal number using place values: 8 8 8 8 8 Place value ± Number =± 8 + 8 + + 8 + 8 + 8 Value 103
Octal system (base 8) Example (1256) 8 in octal is the equivalent to 686 in decimal 8 8 8 8 Place value 1 2 5 6 Number = 1 8 + 2 8 + 5 8 + 6 8 Value = 512 + 128 + 40 + 6 Value 104
Octal system (base 8) Maximum value The maximum value of an octal integer with k digits is equal to n max =8 k -1 Example: if k = 5, n=8 5-1=(32767) 10 It is not common to represent real numbers in octal. We leave it as an exercise. 105
Summary of the four positional systems System Base Symbols Example Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 2345.56 Binary 2 0, 1 (1001.11) 2 Octal 8 0, 1, 2, 3, 4, 5, 6, 7 (156.23) 8 Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F (A2C.A1) 16 106
Comparison of numbers in the Decimal, Binary and Octal systems Decimal Binary Octal 0 000 0 1 001 1 2 010 2 3 011 3 4 100 4 5 101 5 6 110 6 7 111 7 107
Comparison of numbers in the four systems Decimal Binary Octal Hexadecimal 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3 3 4 0100 4 4 5 0101 5 5 6 0110 6 6 7 0111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 108
Conversion Need to know how to convert a number in one system to the equivalent number in another system. We first show how to convert from any base to decimal. Then we show how to convert from decimal to any base. Then, we show how to convert from binary to hexadecimal or octal and vice versa. 109
Conversion from base b to decimal To convert: multiply each digit with its place value in the source system and add the results to get the number in the decimal system. Decimal point System b b b b Place value + + + + + + + Mutiplication Integral Fraction Decimal 110
Conversion Example 1 Convert the binary number (110.11) 2 to decimal. (110.11) 2 = 6.75 Binary 1 1 0 1 1 Place Value 2 2 2 2 2 Partial result 4 + 2 + 0 + 0.5 + 0.25 Decimal 6.75 111
Conversion Example 2 Convert the hexadecimal number (1A.23) 16 to decimal. Hexadecimal 1 A 2 3 Place Value 16 16 16 16 Partial result 16 + 10 + 0.125 + 0.012 Decimal 26.137 =0.1171875 (1A.23) 16 26.137 112
Conversion Example 3 Convert the octal number (23.17) 8 to decimal. Octal 2 3 1 7 Place Value 8 8 8 8 Partial result 16 + 3 + 0.125 + 0.109 Decimal 19.234 =0.109375 (23.17) 8 19.234 113
Conversion from decimal to base b We can convert a decimal number to its equivalent in any base: We need 2 procedures: 1. One for the integral part 2. One for the fractional part 114
Converting the integral part Done by repetitive division. Source number Remainders Quotients Divide by destination base 0 Q Q Q S R R R R D Destination number Destination digits 115
Converting the integral part Example 1 Convert 35 in decimal to binary (base 2) Divide by 2 0 1 2 4 8 17 35 D 1 0 0 0 1 1 35 = (100011) 2 116
Converting the integral part Example 2 Convert 126 in decimal to octal 0 1 15 126 1 7 6 126= (176) 8 117
Converting the integral part Example 3 Convert 126 in decimal to hexadecimal 0 7 126 7 126= (7E) 16 118
Converting the fractional part Done by repetitive multiplication. Source number Integral part Fractional part Multiply by destination base S F F F 0 I I I I Destination number Destination digits 119
Converting the fractional part Convert the decimal number 0.625 to binary 0.625 0.25 0.50 0 I I I 1 0 1 0.625= (0.101) 2 120
Converting the fractional part Convert the decimal number 0.634 to octal (with a maximum of 4 digits) 0.634 0.072 0.576 0.608 0.864 I I I I 5 0 4 4 0.634= (0.5044) 8 121
Converting the fractional part Convert the decimal number 178.6 to hexadecimal 178.6= (B2.9) 16 0 11 178 0.6 0.6 2 9 122
Number of digits In a base b, we can find the number of digits of an integer using = log means the smallest integer greater than or equal to Example: find the required number of bits in the decimal number 234 in all four systems In decimal = log 234 = 2.37 =3 In binary = log 234 = 7.8 =8(because 234=(11101010) 2 ) In octal = log 234 = 2.62 =3(because 234=(352) 8 ) In hexadecimal = log 234 = 1.96 =2(because 234=(EA) 16 ) 123
Binary hexadecimal conversion 4 bits in binary is one digit in hexadecimal B m B m-1 B m-2 B m-3 B 7 B 6 B 5 B 4 B 3 B 2 B 1 B 0 Binary H n H 1 H 0 Hexadecimal 124
Binary hexadecimal conversion Example Show the hexadecimal equivalent of the binary number (10011100010) 2 We arrange the binary number in 4-bit patterns: 100 1110 0010 The leftmost patter can have 1 to 4 bits Change the number to hexadecimal (100 1110 0010) 2 = (4E2) 16 125
Binary hexadecimal conversion Example What s the binary equivalent of (24C) 16 Each hexadecimal digit is converted to 4-bit patterns: 2 0010 4 0100 C 1100 The result (0010 0100 1100) 2 126
Binary octal conversion 3 bits in binary is one octal digit B m B m-1 B m-2 B 5 B 4 B 3 B 2 B 1 B 0 Binary O n O 1 O 0 Octal 127
Binary octal conversion Example Show the octal equivalent of the binary number (101110010) 2 We arrange the binary number in 3-bit patterns: 101 110 010 Change the number to octal (101 110 010) 2 = (562) 8 128
Binary octal conversion Example What s the binary equivalent of (24) 8 Each octal digit is converted to 3-bit patterns: 2 010 4 100 The result (010 100) 2 129
Hexadecimal Binary Octal conversion 3 bits in binary is one octal digit 4 1 1 6 Octal 1 0 0 0 0 1 0 0 1 1 1 0 Binary 8 4 E Hexadecimal 130
Nonpositional number systems Nonpositional number systems are not used in computers. Uses a limited number of symbols in which each symbol has a value. The position a symbol occupies in the number normally bears no relation to its value 131
Nonpositional number systems Roman numerals S={I, V, X, L, C, D, M} Symbol I V X L C D M Value 1 5 10 50 100 500 1000 To find the value of a number: 1. When a symbol with smaller value is placed after a symbol having an equal or larger value, the values are added. 2. When a symbol with smaller value is placed before a symbol having a larger value, the smaller value is subtracted from the larger one. 3. A symbol S 1 cannot come before another symbol S 2 if S 1 10* S 2. Example: I or V cannot come before C. 132
Nonpositional number systems Roman numerals - Examples III 1+1+1 = 3 IV 5-1 = 4 VIII 5+1+1+1=8 XVIII 10+5+1+1+1=18 XIX 10+(10-1)=19 LXXII 50+10+10+1+1=72 CI 100+1=101 MMVII 1000+1000+5+1+1=2007 MDC 1000+500+100=1600 133
References Foundations of Computer Science Behrouz A. Forouzan, Firouz Mosharraf 134
Upcoming lecture number Character typed on keyboard Part of an image Part of a song Part of a film 65 A Program Math routine Program Text editor Program Image recorder Program Music recorder Program Video recorder 01000001 Memory 01000001 Memory 01000001 Memory 01000001 Memory 01000001 Memory 145