ODU CS 419 - Elements of Computer Science

Unformatted text preview:

PowerPoint PresentationOutlineSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Binary AdditionNegative numbers1/4Slide 12Slide 13Slide 14Slide 15Lecture 2: 8/29/2002 CS149D Fall 2002 1CS149D Elements of Computer ScienceAyman Abdel-HamidDepartment of Computer ScienceOld Dominion UniversityLecture 2: 8/29/2002Lecture 2: 8/29/2002 CS149D Fall 2002 2Outline•Number Systems•Decimal/Binary•Octal/Hexadecimal•Binary addition•Representing Negatives•Two’s complement notation and addition•Overflow problemToday’s lecture covers sections 1.4, 1.5, and 1.6 in Brookshear textLecture 2: 8/29/2002 CS149D Fall 2002 3•All computers use the binary number system (base 2)•basic nature of electronic circuits •ON/OFF TRUE/FALSE•current flow/does not flow•Machine alphabet has two letters “0”, “1”•Each letter is a binary digit “bit”. Byte is 8 bits.IntroductionLecture 2: 8/29/2002 CS149D Fall 2002 4Numbers can be represented in any base (humans use base 10)•Symbols for a number system of base B are 0, 1, 2, …, B –1 •decimal (base 10) 0, 1, 2, .., 9 binary (base 2) 0, 1•notation “numberB” (375 in decimal is written 37510, 1011 in binary is written 10112)•Value of ith digit d is “d * Bi” where i starts from 0 and increases from right to left 2 1 0 i positional notation3 7 5 d5 * 100= 57 * 101= 703 * 102= 300Three hundred and seventy fiveNumber SystemsLecture 2: 8/29/2002 CS149D Fall 2002 5Conversion from binary to decimalConvert 10112 to decimal= (1 * 20) + (1 * 21) + (0 * 22) + (1 *23) = 1 + 2 + 0 + 8= 11103 2 1 0 i1 0 1 1 dThis process can be used for conversion from any number system to decimal (TRY convert 1238 to decimal)Lecture 2: 8/29/2002 CS149D Fall 2002 6Conversion from decimal to binaryConvert 1310 to binaryStep 1: divide value by 2 and record remainderStep 2: as long as quotient not zero, continue to divide the newest quotient by 2 and record the remainderStep 3: when obtain a zero as quotient, binary representation consists of remainders listed from right to left in orderOperation Quotient remainder13 by 2 6 16 by 2 3 03 by 2 1 11 by 2 0 11310 = 11012Lecture 2: 8/29/2002 CS149D Fall 2002 7Other Number Systems•Octal (base 8)Symbols (0, 1, 2, 3, 4, 5, 6, 7)•Working with too long binary numbers is a problem•Hexadecimal (base 16)Symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)•Byte = 8 bits = 2 hex digits ( 1 hex digit is 4 bits)B516 = ?2B16 is 10112516 is 01012B516 = 101101012Lecture 2: 8/29/2002 CS149D Fall 2002 8Conversion from binary to hexConvert 11010011102 to hexDivide binary number into 4 bits groups from right to left1101001110316416E1634E1611 0100 1110Lecture 2: 8/29/2002 CS149D Fall 2002 9Decimal Binary Hexadecimal0 0000 01 0001 12 0010 23 0011 34 0100 45 0101 56 0110 67 0111 78 1000 89 1001 910 1010 A11 1011 B12 1100 C13 1101 D14 1110 E15 1111 F20 = 1 27 = 12821 = 2 28 = 25622 = 4 29 = 51223 = 8 210 = 102424 = 16 211 = 204825 = 32 212 = 409626 = 64 213 = 8190Lecture 2: 8/29/2002 CS149D Fall 2002 10Binary AdditionBinary addition table0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 0 and a carry of 1 00111010+00011011 01010101Lecture 2: 8/29/2002 CS149D Fall 2002 11Negative numbers1/4Sign and magnitudeLeft hand bit is used to determine the sign of the numberLeft hand bit 0 = positive numberLeft hand bit 1 = negative number0010 = +21010 = -2Using 4 bits with sign and magnitude, largest positive number represented is 7 (-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7)15 values overall can be representedLecture 2: 8/29/2002 CS149D Fall 2002 12Negative numbers2/4Two’s Complement•+2 added to –2 in sign magnitude using regular binary addition does not equal 0 (make sure of that!)•First bit indicates a negative value but also bears a position value•For a positive number, the two’s complement representation is itself•For a negative number, complement positive value, then add 13 in two’s complement is 011-3 in two’s complement is Invert bits 011 becomes 100Add 1 100 + 1 = 101Invert bits0 becomes 11 Becomes 0Lecture 2: 8/29/2002 CS149D Fall 2002 13What is 1010 in two’s complement?It is a negative number since left hand bit is a 1Invert bits 1010 becomes 0101Add 1 0101 + 1 = 0110 (+6)Then the original number is -6Negative numbers3/4Lecture 2: 8/29/2002 CS149D Fall 2002 14Negative Numbers4/4Two’s complement additionCopyright 2003 Pearson Education, Inc.Subtraction performed the same as addition7-5 is the same as 7+ (-5)1011 = -(0100+1) =-(0101) = -5Lecture 2: 8/29/2002 CS149D Fall 2002 15Overflow ProblemTry adding 5 + 4 in 4-bit two’s complement notation 5 0101+4 0100 9 1001Overflow. Result is Negative valueActually –(0110+1) = -(0111) = -7There is a limit to the size of the values that can be represented according to how many bits are used.Normally integers are represented in 32-bit


View Full Document

ODU CS 419 - Elements of Computer Science

Download Elements of Computer Science
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Elements of Computer Science and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Elements of Computer Science 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?