LECTURE 1-2 Introduction and Number Systems 1
BASIC INFORMATION Course Code: CSE 115 Course Title: Computing Concepts Course Teacher: Dr. Muhammad Asif H. Khan (Mfs), Associate Professor, Dept. of Computer Sci. & Eng., Dhaka University. Text Book 1. Programming in ANSI C E. Balagurusamy 2. Problem Solving and Program Design in C (7 th Edition) -- Jeri R. Hanly & Elliot B. Koffman
REFERENCE BOOKS Other Books You can Look into: 1. C The Complete Reference Herbert Schildt
COURSE WEBSITE www.engrade.com How to use? Go to http://www.engrade.com/students If you do not have an account with Engrade, then Join as a student and provide the Access Code. Collect the Access code from me during the next lab session. If you already have an account, just add a new course by providing the Access Code. Why to Use? All lecture notes and other deliverables will be provided through Engrade.
COURSE EVALUATION Topic Marks Attendance (Randomly checked) 5 Class Performance and HW 10 Quizzes (4 in total) 20 Midterm-1 15 Midterm-2 25 Final 25
WHAT IS A COMPUTER Computer Device capable of performing computations and making logical decisions Computer programs A set of instructions that control computer s processing of data to do a particular work Hardware Various devices comprising computer Software Application programs developed to do a specific set of tasks for the users
INTRODUCTION TO NUMBER SYSTEMS We are all familiar with the decimal number system (Base 10). Some other number systems that we will work with are: Binary Base 2 Octal Base 8 Hexadecimal Base 16 7
CHARACTERISTICS OF NUMBER SYSTEMS 1) The digits are consecutive. 2) The number of digits is equal to the size of the base. 3) Zero is always the first digit. 4) The base number is never a digit. 5) When 1 is added to the largest digit, a sum of zero and a carry of one results. 6) Numeric values determined by the implicit positional values of the digits. 8
SIGNIFICANT DIGITS Binary: 11101101 Most significant digit Least significant digit Hexadecimal: 1D63A7A Most significant digit Least significant digit 9
BINARY NUMBER SYSTEM Also called the Base 2 system Two digits: 0 and 1 The binary number system is used to model the series of electrical signals computers use to represent information 0 represents the no voltage or an off state 1 represents the presence of voltage or an on state 10
BINARY NUMBERING SCALE Base 2 Number Base 10 Equivalent 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Power Positional Value 2 0 1 2 1 2 2 2 4 2 3 8 2 4 16 2 5 32 2 6 64 2 7 128 11
DECIMAL TO BINARY CONVERSION The easiest way to convert a decimal number to its binary equivalent is to use the Division Algorithm This method repeatedly divides a decimal number by 2 and records the quotient and remainder The remainder digits (a sequence of zeros and ones) form the binary equivalent in least significant to most significant digit sequence 13
DIVISION ALGORITHM Convert 67 to its binary equivalent: 67 10 = x 2 Step 1: 67 / 2 = 33 R 1 Step 2: 33 / 2 = 16 R 1 Step 3: 16 / 2 = 8 R 0 Step 4: 8 / 2 = 4 R 0 Step 5: 4 / 2 = 2 R 0 Step 6: 2 / 2 = 1 R 0 Divide 67 by 2. Record quotient in next row Again divide by 2; record quotient in next row Repeat again Repeat again Repeat again Repeat again Step 7: 1 / 2 = 0 R 1 STOP when quotient equals 0 1 0 0 0 0 1 1 2 14
BINARY TO DECIMAL CONVERSION The easiest method for converting a binary number to its decimal equivalent is to use the Multiplication Algorithm Multiply the binary digits by increasing powers of two, starting from the right Then, to find the decimal number equivalent, sum those products 15
MULTIPLICATION ALGORITHM Convert (10101101) 2 to its decimal equivalent: Binary 1 0 1 0 1 1 0 1 x x x x x x x x Positional Values Products 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0 128 +64+ 32+0+8+ 4 +0 + 1 173 10 16
OCTAL NUMBER SYSTEM Also known as the Base 8 System Uses digits 0-7 Readily converts to binary Groups of three (binary) digits can be used to represent each octal digit Also uses multiplication and division algorithms for conversion to and from base 10 17
DECIMAL TO OCTAL CONVERSION Convert 427 10 to its octal equivalent: 427 / 8 = 53 R3 Divide by 8; R is LSD 53 / 8 = 6 R5 Divide Q by 8; R is next digit 6 / 8 = 0 R6 Repeat until Q = 0 653 8 18
OCTAL TO DECIMAL CONVERSION Convert 653 8 to its decimal equivalent: Octal Digits Positional Values Products 6 5 3 x x x 8 2 8 1 8 0 384 + 40 + 3 427 10 19
OCTAL TO BINARY CONVERSION Each octal number converts to 3 binary digits To convert 653 8 to binary, just substitute code: 6 5 3 110 101 011 20
HEXADECIMAL NUMBER SYSTEM Base 16 system Uses digits 0-9 & letters A,B,C,D,E,F Groups of four bits represent each base 16 digit 21
DECIMAL TO HEXADECIMAL CONVERSION Convert 830 10 to its hexadecimal equivalent: 830 / 16 = 51 R14 51 / 16 = 3 R3 3 / 16 = 0 R3 = E in Hex 33E 16 22
HEXADECIMAL TO DECIMAL CONVERSION Convert 3B4F 16 to its decimal equivalent: Hex Digits Positional Values Products 3 B 4 F x x x x 16 3 16 2 16 1 16 0 12288 +2816 + 64 +15 15,183 10 23
BINARY TO HEXADECIMAL CONVERSION The easiest method for converting binary to hexadecimal is to use a substitution code Each hex number converts to 4 binary digits 24
SUBSTITUTION CODE Convert 010101101010111001101010 2 to hex using the 4-bit substitution code : A 5 6 A E 6 0101 0110 1010 1110 0110 1010 56AE6A 16 25
SUBSTITUTION CODE Substitution code can also be used to convert binary to octal by using 3-bit groupings: 25527152 8 26
SUBTRACTION BY ADDITION Follow these steps: take the "complement" of the number you are subtracting (I will show you how) add it to the number you are subtracting from discard the extra "1" on the left 27
COMPLEMENT The "complement" is the number to add to make 10 (or 100, 1000, etc, depending on how many digits you have) Example The complement of 3 is 7, because 3+7=10 (you add 7 to make 10) Example: the complement of 85 is 15, because 85+15=100 Example: the complement of 111 is 889, because 111+889=1000 28
CALCULATING THE COMPLEMENT The basic idea is to find the difference between each digit and 9. That will get you to "999...", so you only need to add 1 to make it "1000... Steps Starting at the right (the "units" position) Skip over any zeros at the start For the first digit that isn't zero: find what would make it to 10 For all other digits: find what would make it to 9 What is the complement of 1700? 29
SUBTRACTION BY ADDITION Follow these steps: take the "complement" of the number you are subtracting add it to the number you are subtracting from discard the extra "1" on the left 30
SUBTRACTION BY ADDITION Example Find 653-372. Complement of 372 is 628 (verify yourself) 653 + 628 = 1281 Discard the leading 1, and that s your answer! 31
WHAT IF THE NUMBER YOU ARE SUBTRACTING HAS LESS DIGITS? How would you, for example, do 4567-56? the complement of 56 is 44, but we need to "pad it" out to 4 digits, so we end up with 9944. 4567 +9944 14511 32
COMPLEMENT Complement is the negative equivalent of a number. If we have a number N then complement of N will give us another number which is equivalent to N So if complement of N is M, then we can say M = -N So complement of M = -M = -(-N) = N So complement of complement gives the original number 33
TYPES OF COMPLEMENT For a number of base r, two types of complements can be found 1. r s complement 2. (r-1) s complement Definition: If N is a number of base r having n digits then r s complement of N = r n N and (r-1) s complement of N = r n -N-1 34
EXAMPLE Suppose N = (3675) 10 So we can find two complements of this number. The 10 s complement and the 9 s complement. Here n = 4 10 s complement of (3675) = 10 4-3675 = 6325 9 s complement of (3675) = 10 4-3675 -1 = 6324 35
SHORT CUT WAY TO FIND (R-1) S COMPLEMENT In the previous example we see that 9 s complement of 3675 is 6324. We can get the result by subtracting each digit from 9. Similarly for other base, the (r-1) s complement can be found by subtracting each digit from r-1 (the highest digit in that system). For binary 1 s complement is even more easy. Just change 1 to 0 and 0 to 1. (Because 1-1=0 and 1-0=1) 36
EXAMPLE: Find the (r-1) s complement in short cut method. (620143) 8 Ans: 157634 (A4D7E) 16 Ans: 5B281 (110100101) 2 Ans: 001011010 37
SHORT CUT WAY TO FIND R S COMPLEMENT From the definition we can say, r s complement of (N) = (r-1) s complement +1 So, we can first find the (r-1) s complement in short cut way then add 1 to get the r s complement. Example: r s complement of (620143) 8 =157634 + 1 = 157635 This method is a two step process. But we can find it in one step process also. 38
SHORT CUT WAY TO FIND R S COMPLEMENT One step process: Start from rightmost digit to left. Initial zeros will remain unchanged Rightmost non-zero digit will be subtracted from r Rest of the digits will be subtracted from r-1 Example: Find the 10 s complement of (529400) 10 Rightmost 2 zeros will not change, 4 will be subtracted from 10 and rest of the digits 529 will be subtracted from 9 So the result is 470600 39
Example Find the r s complement in short cut method. (8210) 10 Ans: 1790 (61352) 10 Ans: 38648 (6201430) 8 Ans: 1576350 (A4D7E0) 16 Ans: 5B2820 40
EXAMPLE FOR BINARY For binary: start from rightmost bit Up to first 1 no change. For rest of the bits toggle (Change 1 to 0 and 0 to 1) (11010010100) 2 Ans: 00101101100 (01101001011) 2 Ans: 10010110101 (10000000) 2 Ans: 10000000 41
USE OF COMPLEMENT Complement is used to perform subtraction using addition Mathematically A-B = A + (-B) So we can get the result of A-B by adding complement of B with A. So A-B = A + Complement of (B) 42