Mathematics for Computing and Informatics 1A (MCI511S) Lecture Notes 2021 By G. Tapedzesa 2021 TAPEDZESA G., NUST 1 CHAPTER 1: NUMBER SYSTEMS Learning Outcomes Upon completion of this unit, you will be able to: • Recognize different types of number systems as they relate to computers • Describe the decimal, binary, hexadecimal and octal system • Identify and define a base/radix, power, positional notation, and most and least significant digits as they relate to decimal, binary, octal, and hexadecimal number systems • Convert a number in binary, octal or hexadecimal to a number in the decimal system. • Convert a number in the decimal system to a number in binary, octal and hexadecimal. • Perform arithmetic operations in binary, octal, and hexadecimal number systems 2021 TAPEDZESA G., NUST 2 NUMBER SYSTEMS: Introduction • A number system defines how a number can be represented using distinct symbols. • A number can be represented differently in different systems represented differently in different systems • The number system that you are familiar with, that you use every day, is the decimal number system, also commonly referred to as the base-10 positional number system. • When you perform computations such as 3 + 2 = 5, ππ 21 – 7 = 14 you are using the decimal number system. • This decimal system is ingrained into your subconscious; it’s the natural way that you think about numbers. 2021 TAPEDZESA G., NUST 3 • Computers can most readily use two symbols, and therefore a base-2 system, or binary number system, is most appropriate. • The base-2 system has exactly two symbols: 0 and 1. • The base-10 symbols are termed digits. The base-2 symbols are termed binary digits, or bits for short. • All base-10 numbers are built as strings of digits (such as 6349). • All binary numbers are built as strings of bits (such as 1101). Just as we would say that the decimal number 12890 has five digits, we would say that the binary number 11001 is a five-bit number. 2021 TAPEDZESA G., NUST 4 The Decimal Number System The decimal (base 10) system contains ten digits denoted by the symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Any positive integer in the decimal system can be expressed, in expanded notation, as a sum of powers of 10 multiplied by a digit. For instance, 8253 = 8 × 103 + 2 × 102 + 5 × 101 + 3 × 100 2021 TAPEDZESA G., NUST 5 The Decimal Number System • Any fractional number, represented in the decimal system by a string of decimal digits together with an embedded decimal point may also be expressed in expanded notation by using negative powers of 10. For example, πππ. πππ = π × πππ + π × πππ + π × πππ + π × ππ−π + π × ππ−π + π × ππ−π • This decimal fraction is said to have three decimal places, which is the number of digits to the right of the decimal point 2021 TAPEDZESA G., NUST 6 Face Value and Place Value • The face value or inherent value of a symbol is the value of that symbol standing alone • For example, the face value of 6 in numbers 256, 165, 698 is 6 even if it is used in different number positions • The place value or positional value of a numeric symbol is directly related to the base of a system • In the case of the decimal system, each position has a value of 10 times greater than the position to its right • For example: in the number 423, the symbol 3 represents the units (100), the symbol 2 represents the tens position (101), and the symbol 4 represents the hundreds position (102). 2021 TAPEDZESA G., NUST 7 Binary System • All data in a computer is represented in binary. The pictures stored on your hard drive—it’s all bits. The YouTube video of the cat falling off the chair that you saw this morning—bits. Your Facebook page—bits. The tweet you sent—bits. Everything is bits. • To understand how computers work, you have to speak the language. And the language of computers is the binary number system. 2021 TAPEDZESA G., NUST 8 The Binary Number System • The binary system is a positional numeration system to the base 2. • Those binary numbers that have no fractional part are called binary integers. • A string of eight bits (such as 11000110) is termed a byte. • The table in the next slide gives some idea of the correspondence between binary numbers and the more familiar decimal numbers. 2021 TAPEDZESA G., NUST 9 You should “memorize” the binary representations of the decimal digits 0 through 15 shown below • Here we used the 4-bit representation. • When you are given the size of the storage location, include the leading zeros to show all bits in the storage location. • For example, if told to represent decimal 5 as an 8-bit binary number, your answer should be 00000101 2021 TAPEDZESA G., NUST 10 •The idea of describing numbers using a positional system, as we have illustrated for base-10 and base-2, can be extended to any base. •To avoid confusion when the base in use if not clear from the context, or when using multiple bases in a single expression, we append a subscript to the number to indicate the base, and write: 510 = 1012 2021 TAPEDZESA G., NUST 11 You should, henceforth, be able to readily shift between the binary and decimal number representations. 2021 TAPEDZESA G., NUST 12 Binary-to-Decimal Conversion • To convert a binary number to a decimal number, we simply write the binary number as a sum of powers of 2. For example, 10112 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 = 8 + 2 + 1 = 1110 1101012 = 1 × 25 + 1 × 24 + 0 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 32 + 16 + 4 + 1 = 5310 Exercise: Convert each of the following binary numbers as decimal numbers 1101102 , 10000012 , 101.11012 2021 TAPEDZESA G., NUST 13 Decimal-to-Binary Conversion • Given a binary number, you can now convert it to the equivalent decimal number. • The method of converting a decimal number to a binary number, which is much more readily adaptable to programming on a computer, entails repeatedly dividing the decimal number by 2, keeping track of the remainder at each step. • To convert the decimal number, say x, to binary we follow the steps on the next slide: 2021 TAPEDZESA G., NUST 14 Step 1. Divide π₯ by 2 to obtain a quotient and remainder. The remainder will be 0 or 1. Step 2. If the quotient is zero, proceed to Step 3. Otherwise, go back to Step 1, assigning π₯ to be the value of the most-recent quotient from Step 1. Step 3. The sequence of remainders from bottom up forms the binary representation of the number. Example: Convert the decimal number 53 to its binary equivalent. 53 ÷ 2 = 26 π 1 26 ÷ 2 = 13 π 0 13 ÷ 2 = 6 π 1 6÷2=3π0 3÷2=1π1 1÷2=0π1 Ans = 110101 2021 TAPEDZESA G., NUST 15 • If a decimal number has a fractional part, you convert the integral part and the fractional part separately as illustrated with the decimal number 109.78125 below. • First, convert the integral part 109 to its binary equivalent: 109 = 11011012 (verify) • Then convert the fractional part 0.78125 to its binary equivalent by successively multiplying the fractional part by 2, noting the integral part of the product, as follows: 2021 TAPEDZESA G., NUST 16 Decimal-to-Binary Conversion 0.78125 × 2 = π. 56250 0.5625 × 2 = π. 1250 0.125 × 2 = π. 250 0.25 × 2 = π. 50 0.5 × 2 = π. 0 You stop when you have a zero fractional part. The sequence of integral part digits (in red) from top down yields the required binary representation. That is, 0.78125 = 0.110012 The binary equivalents of the decimal number 109.78125 is the sum of the binary equivalents of its integral and fractional parts: 109.78125 = 1101101.110012 2021 TAPEDZESA G., NUST 17 Binary Arithmetic The execution of numerical calculations is essentially the same in all positional numeration systems. Binary Addition The algorithm for the addition of decimal numbers holds also for the addition of binary numbers; the only addition facts needed for binary addition are: 0+0=0 0+1=1 1+0=1 1 + 1 = 0, with a carry of 1 1 + 1 + 1 = 1, with a carry of 1 2021 TAPEDZESA G., NUST 18 Binary Addition Go through examples 1.5 and 1.6 in the prescribed textbook. Exercise: In each of the following cases, perform the addition of the binary numbers: 1. 2. 2021 TAPEDZESA G., NUST 19 Binary Multiplication The algorithm for decimal multiplication also holds for binary multiplication. Please note that when performing the operation of multiplication, it is extremely important to line up the numbers in the correct columns. For practice, attempt examples 1.8 and 1.9 in the prescribed textbook 2021 TAPEDZESA G., NUST 20 Binary Subtraction The same “borrowing” procedure followed in the decimal system is applied in the binary system. The only subtraction tion facts needed for binary subtraction are: 0−0=0 1−0=1 1−1=0 10 − 1 = 1 0 − 1 = 1, with a borrow of 1 from the next column Examples 1.12 – 1.14 2021 TAPEDZESA G., NUST 21 Binary Division • The algorithm for binary division is simply repeated subtraction of the divisor. • As in decimal division of integers, a remainder is possible when one binary integer is divided by another. • Also, the division of binary fractions is handled the same way as the division of decimal fractions. 2021 TAPEDZESA G., NUST 22 Complements • All computers represent integers in a manner that allows both positive and negative numbers • Remember: (including the negative sign) must be represented as bits; that is, ones and zeros! • Many computers store negative numbers in the form of their arithmetic complements • Complements are used to reduce subtraction to addition • First we discuss these complements in the familiar decimal system, known as the nines complement and the tens complement 2021 TAPEDZESA G., NUST 23 Decimal Complements The nines complement of a decimal number, say A, is obtained by subtracting each digit of A from 9; and the tens complement of A is its nines complement plus one. Example 2021 Decimal number Nine complement Tens complement 4308 5691 5692 123 123 876 876 876 877 9672 0327 0328 751 620 248 379 248 380 TAPEDZESA G., NUST 24 • To illustrate the use of complements in subtraction, let A and B be two decimal integers with the same number of digits, say 4 digits, and suppose that A is less than B • We can simplify the difference π΅ − π΄ as π΅ − π΄ = π΅ − π΄ + 10 000 − 10 000 = π΅ − π΄ + (9999 + 1 − 10 000) = π΅ + (9999 − π΄ + 1) − 10 000 = π΅ + 9999 − π΄ + 1 − 10 000 Thus, we can calculate B − π΄ either by adding the tens complement of A to B and then subtracting 10 000, or by adding the nines complement of A and then adding 1 and subtracting 10 000. Attempt example 1.18 2021 TAPEDZESA G., NUST 25 • Suppose now that A is greater than B. For instance, let us consider A=5872 and B=2148. Then the difference A-B is negative: • We can simplify the difference π΅ − π΄ as π΅ − π΄ = 2148 + (9999 − 5872 + 1) − 10 000 = 2148 + 4128 − 10 000 = −3724 2021 TAPEDZESA G., NUST 26 Binary Complements • The terminology and principle of complements in the decimal system can be easily translated into the binary system • Specifically, if A is a binary number, the ones complement of A is obtained by subtracting each digit of A from 1, and the twos complement of A is its ones complement plus 1. Example 2021 Binary number Ones complement Twos complement 11011100 00100011 00100100 00100111 11011000 11011001 00011000 11100111 11101000 TAPEDZESA G., NUST 27 Binary Complements • Observe that taking the ones complement simply inverts each digit, that is 0 is replaced by 1 and vice versa. • Specifically, if A is a binary number, the ones complement of A is obtained by subtracting each digit of A from 1, and the twos complement of A is its ones complement plus 1. Example Binary number Ones complement Twos complement 11011100 00100011 00100100 00100111 11011000 11011001 Attempt examples 1.20 and 1.21 in the11101000 prescribed 00011000 11100111 textbook 2021 TAPEDZESA G., NUST 28 So, to find the representation of a negative decimal number, do the following: • First find the binary representation of the number without the negative sign • Then take the two’s complement. The result is the representation of the negative number. Every computer today uses two’s complement notation for the storage of integers. Note: a binary number, added to its two’s complement, results in zero, as expected. 2021 TAPEDZESA G., NUST 29 Tutorial Exercises 1. Find the face value and the place value of the digit 4 in the following decimal numbers. (a) 7425 (b) 146 723 (c) 305.54 (d) 0.012345 2. Rewrite in expanded notation. a) 2468 (b) 54.321 3. Give the face value and the place value of each of the underlined bit: (a) 101102 (b) 10110012 (c) 101.110102 (d) 110.00102 4. Rewrite in expanded notation. a) 1101102 (b) 11.011012 2021 TAPEDZESA G., NUST 30 5. Convert each binary number to its decimal form: (a) 11100112 (b) 110.10112 6. Convert the decimal number 437.206 251 to its binary equivalent. 7. Evaluate the following binary sums: a) 11011 + 1010 (b) 110.1101 + 1011.011 8. Evaluate the following binary products: a) 110111 × 101 (b) 110.001 × 1.11 9. Evaluate the following binary differences: a) 1110001 − 111011 (b) 1101.0011 − 110.11011 10. Evaluate 111001 ÷ 1001 to two binary places. 2021 TAPEDZESA G., NUST 31 11. Determine the nines and tens complements of the following decimal numbers: (a) 3268 (b) 479 200 (c) 99 132 756 12. Find the following differences using tens complements: (a) 53 726 − 14 503 (b) 2658 − 4321 13. Determine the ones and twos complements of the binary numbers: a) 11011 + 1010 (b) 110.1101 + 1011.011 14. Evaluate the following binary differences using twos complements: a) 11011 − 1010 b) 111000 − 110100 c) 101010011 − 111011001 d) 110.1101 + 1011.011 2021 TAPEDZESA G., NUST 32 • Computer engineers must oftentimes look at the contents of a specific item in computer memory. • You might, for instance, have to look at a variable that is stored at an address: 00000000000100101111111101111100 • Surely, such long binary strings would be cumbersome to transcribe or to read off to a colleague. • Even if you have come to love the binary number system, you would still likely agree that these long strings are too much of a good thing. 2020 TAPEDZESA G., NUST 33 • Fortunately, large binary numbers can be made much more compact, and hence easier to work with, if represented in other number systems, the octal (base 8) and the hexadecimal (base 16) systems. 8 = 23 , 16 = 24 • Since 8 and 16 are powers of 2, there is almost instant interconversion between the octal and hexadecimal systems and the binary system. • Although the decimal system is more compact than the binary system, the octal and hexadecimal systems are in fact comparable in compactness with the decimal system • Converting between binary and hexadecimal is exceedingly easy— much easier than converting between binary and decimal. 2020 TAPEDZESA G., NUST 34 Converting from any base to base 10 • We can convert a base π number, ππ , into its decimal representation by writing ππ in expanded notation and calculating by decimal arithmetic • This conversion is accomplished by the following algorithm, which distinguishes between the integral part and the fractional part of the number: Integral part: Multiply the leftmost digit by the base π and add the next digit to the right. Multiply the sum by the base π and add the next digit. Repeat the process until the rightmost digit is added. The final answer is the required decimal integral part. 2020 TAPEDZESA G., NUST 35 Converting from any base to base 10 Examples 1. Convert the octal number 1753 to its base 10 equivalent 17538 = 1 × 83 + 7 × 82 + 5 × 81 + 3 × 80 = 100310 2. Convert the quintary (base-5) number 142 to its base- 10 equivalent 1425 = 1 × 52 + 4 × 5 + 2 = 47 3. Convert the base-4 number 132.12 to its decimal equivalent 132.124 = 1 × 42 + 3 × 4 + 2 + 1 × 4−1 + 2 × 4−2 = 20.555555556 2020 TAPEDZESA G., NUST 36 Converting from any base π to base 10 Fractional part: Multiply the rightmost digit by the base 1Τπ and add the next digit to the right. Multiply the sum by the base 1Τπ and add the next digit. Repeat the process until the leftmost digit is added and the sum is 1 multiplied by Τπ. The final answer is the required decimal fractional part. 2020 TAPEDZESA G., NUST 37 Examples: a) Convert the quintary (base-5) number 3412 to its decimal equivalent. 34125 = 3 × 53 + 4 × 52 + 1 × 51 + 2 × 50 = 48210 b) Convert the number 56237 to its base-10 equivalent 56237 = 5 × 73 + 6 × 72 + 2 × 71 + 3 × 70 = 202610 c) Convert the number 231.20123 to its decimal equivalent 231.20123 = 2 × 32 + 3 × 31 + 1 × 30 + 2 × 3−1 + 1 × 3−3 + 2 × 3−4 = 28.72 … …10 2020 TAPEDZESA G., NUST 38 Example: Consider the quintary number 2401.23145 . 2401.23145 = 2020 TAPEDZESA G., NUST 39 Decimal-to-base-b Conversion We can convert a decimal number to its base-b representation using the following algorithm, which also distinguishes between the integral part of the number and its fractional part. Integral part: Divide the decimal number and each succeeding quotient by b until a zero quotient is obtained. The sequence of remainders, from bottom up, yields the required base-b representation. Fractional part: Multiply the decimal number and the fractional part of each succeeding product by b until a zero fractional part is obtained. Then the finite (or infinite repeating) sequence of integral parts of the products gives the base-b digit representation, from top down, yields the required base-b representation. 2020 TAPEDZESA G., NUST 40 Decimal-to-base-b Conversion Example 1. Convert the decimal number 1589 to its base-7 equivalent 1589 ÷ 7 = 227 π 0 227 ÷ 7 = 32 π 3 32 ÷ 7 = 4 π 4 4÷7=0 π 4 Therefore 158910 = 44307 2. Convert the decimal number 2546 to its base-5 equivalent 2546 ÷ 5 = 509 π 1 509 ÷ 5 = 101 π 4 101 ÷ 5 = 20 π 1 20 ÷ 5 = 4 π 0 4÷5=0 π4 Therefore 25465 = 40141 2020 TAPEDZESA G., NUST 41 Decimal-to-base-b Conversion Example 1. Convert the decimal number 15.89 to its base-7 equivalent Integer part = 15: 15 ÷ 7 = 2 π 1 2÷7=0 π 2 15 = 217 fractional part = 0.89: 0.89 × 7 = 6.23 0.23 × 7 = 1.61 0.61 × 7 = 4.27 0.27 × 7 = 1.89 0.89 × 7 = 6.23 0.89 = 0. 6αΆ 1αΆαΆ 4αΆ 1αΆ αΆ αΆ 4αΆ 1αΆ Ultimately, 15.89 =21. 61 2020 TAPEDZESA G., NUST 42 Example 1: Convert the decimal number 684 to its quintal (base 5) representation. Solution: We divide 684, and each subsequent quotient, by 5, noting all remainders. Divisions Quotients Remainders 684 ÷ 5 136 4 136 ÷ 5 27 1 27 ÷ 5 5 2 5÷5 1 0 1÷5 0 1 The sequence of remainders from bottom up gives the quintal form for 68410 = 102145 . 2020 TAPEDZESA G., NUST 43 Example 2: Convert the decimal number 0.4704 to its quintal (base 5) representation. Solution: We multiply 0.4704, and each subsequent fractional part, by 5, noting the integral part of each product. Multiplications Products Integral parts 0.4704 × 5 2.352 2 0.352 × 5 1.76 1 0.76 × 5 3.8 3 0.8 × 5 4.0 4 The sequence of integral parts from top down yields the required quintal form for 0.470410 = 0.21345 . 2020 TAPEDZESA G., NUST 44 Octal and Hexadecimal System In the base-8 number system we use the symbols 0 1 2 3 4 5 6 7 Binary to Octal conversions Exercise: Convert the base-6 number 1542 to its base-8 equivalent Holiday exercise 1. Convert the binary number 001 011 010 to its octal equivalent (8 = 23 ) 1011010 = 1328 1. Convert the binary number 011 011 110 to its octal equivalent (8 = 23 ) 11011110 = 3368 In the hexadecimal (base-16) number system we use the symbols 0 1 C 2020 2 D 3 E 4 F 5 6 7 8 9 A B π΄πΉ234 + 100π΅πΆ = TAPEDZESA G., NUST 45 Octal and Hexadecimal System In the base-8 number system we use the symbols 0 1 2 3 4 5 6 7 Octal to Binary conversions 1. Convert the octal number 567 to its binary equivalent 5678 = 1011101112 2. Convert the binary number 01110001.1100 to its hexadecimal equivalent 1110001.11 = 71. πΆ16 3. Convert the binary number 10011101.1111 to its hexadecimal equivalent 10011101.1111 = 9π·. πΉ16 In the hexadecimal (base-16) number system we use the symbols 0 1 C 2 D 3 E 4 F 5 6 7 8 9 A B π¨ππππ + ππππ©πͺ = 2020 TAPEDZESA G., NUST 46 Octal and Hexadecimal System The base-8 and base-16 systems are special: 8 = 23 , 16 = 24 Converting from binary to octal 1. Convert the binary number 011 010 001 to its octal equivalent 110100012 = 3218 2. Convert the binary number 011 011.110 to its octal equivalent 11011.112 = 33.68 Converting from octal to binary 1. Convert the octal number 751 to its binary equivalent 7518 = 1111010012 2. Convert the octal number 6540.102 to its binary equivalent 6540.1028 = 110101100000.0010000102 2020 TAPEDZESA G., NUST 47 Octal and Hexadecimal System The base-8 and base-16 systems are special: 8 = 23 , 16 = 24 Converting from binary to hexadecimal 1. Convert the binary number 0010 1111 1011 to its hexadecimal equivalent 10111110112 = 2πΉπ΅16 2. Convert the binary number 0010 1101 0101.1011 1000 to its hexadecimal equivalent 1011010101.101112 = 2π·5. π΅816 Converting from hexadecimal to binary 1. Convert the hexadecimal number BED0.5B to its binary equivalent π΅πΈπ·0.5π΅ = 1011111011010000.010110112 2 2020 TAPEDZESA G., NUST 48 General Arithmetic Octal: 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22… Decimal: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18… 1.Evaluate the octal addition: 7512+4532 71 512 +4 532 14 244 Therefore, 7512+4532=14244 in base-8 2. Evaluate the octal addition: 157.230 + 254.011 11 51 7.230 +254.011 433.241 Therefore, 157.230 + 254.011 = 433.241 in base-8 2020 TAPEDZESA G., NUST 49 General Arithmetic Base-7: 0 1 2 3 4 5 6 10 11 12 13 14 15 16 20 21 22 23 24… Base-10: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18… 1.Evaluate the base-7 subtraction: 512-453=26 in base-7 1 54 10 2 −4 5 3 26 Base-3: 0 1 2 10 11 12 20 21 22 100 101 102 110 111 112 120 121 122 200… Base-10: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 … 2. Evaluate the base-3 subtraction: 121.2 - 102.11=12.02 in base-3 121 1.21 0 −102.11 12.02 2020 TAPEDZESA G., NUST 50 General Arithmetic Hex: 0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F… Dec: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31… 1. Evaluate the base-16 addition F3A9+2D0E πΉ3π΄9 +2π·0πΈ 120π΅7 Subtraction 1.Evaluate the hexadecimal subtraction: F3A9.B2-2D0E.FF πΈ1 391 8. π΄1 2 −2π·0πΈ. πΉπΉ πΆ69π΄. π΅3 2020 TAPEDZESA G., NUST 51 General Arithmetic 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 1. Evaluate the base-16 addition F3A9+2D0E πΉ3π΄9 +2π·0πΈ 120π΅7 Subtraction 1.Evaluate the hexadecimal subtraction: F3A9.B2-2D0E.FF πΈ1 391 8. π΄1 2 −2π·0πΈ. πΉπΉ πΆ69π΄. π΅3 2020 TAPEDZESA G., NUST 52