Download presentation
Presentation is loading. Please wait.
Published byMaurice Todd Modified over 8 years ago
1
Lecture 2: 8/29/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 2: 8/29/2002
2
CS149D Fall 20022 Outline Number Systems Decimal/Binary Octal/Hexadecimal Binary addition Representing Negatives Two’s complement notation and addition Overflow problem Today’s lecture covers sections 1.4, 1.5, and 1.6 in Brookshear text
3
Lecture 2: 8/29/2002CS149D Fall 20023 All computers use the binary number system (base 2) basic nature of electronic circuits ON/OFFTRUE/FALSE current flow/does not flow Machine alphabet has two letters “0”, “1” Each letter is a binary digit “bit”. Byte is 8 bits. Introduction
4
Lecture 2: 8/29/2002CS149D Fall 20024 Numbers 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,.., 9binary (base 2) 0, 1 notation “number B ” (375 in decimal is written 375 10, 1011 in binary is written 1011 2 ) Value of i th digit d is “d * B i” where i starts from 0 and increases from right to left 2 1 0ipositional notation 3 7 5d 5 * 10 0 =5 7 * 10 1 =70 3 * 10 2 =300 Three hundred and seventy five Number Systems
5
Lecture 2: 8/29/2002CS149D Fall 20025 Conversion from binary to decimal Convert 1011 2 to decimal = (1 * 2 0 ) + (1 * 2 1 ) + (0 * 2 2 ) + (1 *2 3 ) = 1 + 2 + 0 + 8 = 11 10 3 2 1 0i 1 0 1 1 d This process can be used for conversion from any number system to decimal (TRY convert 123 8 to decimal)
6
Lecture 2: 8/29/2002CS149D Fall 20026 Conversion from decimal to binary Convert 13 10 to binary Step 1: divide value by 2 and record remainder Step 2: as long as quotient not zero, continue to divide the newest quotient by 2 and record the remainder Step 3: when obtain a zero as quotient, binary representation consists of remainders listed from right to left in order OperationQuotientremainder 13 by 2 6 1 6 by 2 3 0 3 by 2 1 1 1 by 2 0 1 13 10 = 1101 2
7
Lecture 2: 8/29/2002CS149D Fall 20027 Other 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) B5 16 = ? 2 B 16 is 1011 2 5 16 is 0101 2 B5 16 = 10110101 2
8
Lecture 2: 8/29/2002CS149D Fall 20028 Conversion from binary to hex Convert 1101001110 2 to hex Divide binary number into 4 bits groups from right to left 1101001110 3 16 4 16 E 16 34E 16 1101001110
9
Lecture 2: 8/29/2002CS149D Fall 20029 DecimalBinaryHexadecimal 00000 0 10001 1 20010 2 30011 3 40100 4 50101 5 60110 6 70111 7 81000 8 91001 9 101010 A 111011 B 121100 C 131101 D 141110 E 151111 F 2 0 = 12 7 = 128 2 1 = 22 8 = 256 2 2 = 42 9 = 512 2 3 = 82 10 = 1024 2 4 = 162 11 = 2048 2 5 = 322 12 = 4096 2 6 = 642 13 = 8190
10
Lecture 2: 8/29/2002CS149D Fall 200210 Binary Addition Binary addition table 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 and a carry of 1 00111010 +00011011 01010101
11
Lecture 2: 8/29/2002CS149D Fall 200211 Negative numbers 1/4 Sign and magnitude Left hand bit is used to determine the sign of the number Left hand bit 0 = positive number Left hand bit 1 = negative number 0010 = +2 1010 = -2 Using 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 represented
12
Lecture 2: 8/29/2002CS149D Fall 200212 Negative numbers 2/4 Two’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 1 3 in two’s complement is 011 -3 in two’s complement is Invert bits011 becomes 100 Add 1100 + 1 = 101 Invert bits 0 becomes 1 1 Becomes 0
13
Lecture 2: 8/29/2002CS149D Fall 200213 What is 1010 in two’s complement? It is a negative number since left hand bit is a 1 Invert bits1010 becomes 0101 Add 10101 + 1 = 0110 (+6) Then the original number is -6 Negative numbers 3/4
14
Lecture 2: 8/29/2002CS149D Fall 200214 Negative Numbers 4/4 Two’s complement addition Copyright 2003 Pearson Education, Inc. Subtraction performed the same as addition 7-5 is the same as 7+ (-5) 1011 = -(0100+1) = -(0101) = -5
15
Lecture 2: 8/29/2002CS149D Fall 200215 Overflow Problem Try adding 5 + 4 in 4-bit two’s complement notation 50101 +40100 91001 Overflow. Result is Negative value Actually –(0110+1) = -(0111) = -7 There 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 patterns.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.