Download presentation
Presentation is loading. Please wait.
Published byAugustine Cummings Modified over 9 years ago
1
Number Systems Computer Science 210 Computer Organization
2
People Decimal Numbers (base 10) Sign-Magnitude (-324) Decimal Fractions (23.27) Letters for text
3
Computers Binary Numbers (base 2) Sign-magnitude Binary fractions and floating point ASCII codes for characters (A 65)
4
Why binary? Information is stored in computer via voltage levels. Using decimal would require 10 distinct and reliable levels for each digit. This is not feasible with reasonable reliability and financial constraints. Everything in computer is stored using bits: numbers, text, programs, pictures, sounds, videos,...
5
Decimal: Non-negatives Base 10 Uses decimal digits: 0, 1,..., 9 Positional System - position gives power Example: 3845 = 3x10 3 + 8x10 2 + 4x10 1 + 5x10 0 Positions: …543210
6
Binary: Non-negatives Base 2 Uses binary digits (bits): 0,1 Positional system Example: 1101 = 1x2 3 + 1x2 2 + 0x2 1 + 1x2 0
7
Conversions ExternalInternal (For people) (For computer) 25 11001 A01000001 People want to see and enter numbers in decimal. Computers must store and compute with bits.
8
Binary to Decimal Conversion Algorithm: –Expand binary number using positional scheme. –Perform computation using decimal arithmetic. Example: 11001 2 1x2 4 + 1x2 3 + 0x2 2 + 0x2 1 + 1x2 0 = 2 4 + 2 3 + 2 0 = 16 + 8 + 1 = 25 10
9
Decimal to Binary - Algorithm 1 Algorithm: While N 0 do Set N to N/2 (whole part) Record the remainder (1 or 0) Set A to remainders in reverse order
10
Decimal to binary - Example Example: Convert 324 10 to binary N Rem N Rem 324 162050 81021 40110 20001 100 324 10 = 101000100 2
11
Decimal to Binary - Algorithm 2 Algorithm: Set A to 0 (all bits 0) While N 0 do Find largest P with 2 P N Set bit in position P of A to 1 Set N to N - 2 P
12
Decimal to binary - Example Example: Convert 324 10 to binary N Power P A. 324 2568 100000000 68 646 101000000 4 42 101000100 0 324 10 = 101000100 2
13
Binary Addition One bit numbers: + 0 1 0 | 0 1 1 | 1 10 Example 1111 1 110101 (53) + 101101 (45) 1100010 (98)
14
Overflow In a given type of computer, the size of integers is a fixed number of bits. 16 or 32 bits are popular choices It is possible that addition of two n bit numbers yields a result requiring n+1 bits. Overflow is the term for an operation whose results exceeds the size allowed for a number.
15
Negatives: Sign-Magnitude With a fixed number of bits, say N –The leftmost bit is used to give the sign 0 for positive number 1 for negative number –The other N-1 bits are for the magnitude Example: -25 with 8 bit numbers – Sign: 1 since negative – Magnitude: 11001 for 25 – 8-bit result: 10011001 Note: This would be 153 as a positive.
16
Ranges for N-bit numbers Unsigned (positive) – 0000…00 or 0 – 1111…11 which is 2 N -1 – For N=8, 0 - 255 Sign-magnitude – 1111…11which is -(2 N-1 -1) – 0111…11which is 2 N-1 -1 – For N=8, -127 to 127 2 ’ s Complement – 1000…00 which is -2 N-1 – 0111…11which is 2 N-1 - 1 – For N=8, -128 to 127
17
Octal Numbers Base 8 Digits 0,1,2,3,4,5,6,7 Not so many digits as binary Easy to convert to and from binary Often used by people who need to see the internal representation of data, programs, etc.
18
Octal Conversions Octal to Binary – Simply convert each octal digit to a three bit binary number. – Example: 536 8 = 101 011 110 2 Binary to Octal – Starting at right, group into 3 bit groups – Convert each group to an octal digit – Example 11011111101010 2 = 011 011 111 101 010 = 33752 8
19
Hexadecimal Base 16 Digits 0,…,9,A,B,C,D,E,F Hexadecimal Binary – Just like Octal, only use 4 bits per digit. Example: 98C3 16 = 1001 1000 1100 0011 2 Example 11010011101011 2 = 0011 0100 1110 1011 = 34EB
20
Sign-Magnitude: Pros and Cons Pro: –Easy to comprehend –Easy to convert Con: –Addition complicated (expensive) If signs same then … else if positive part larger … –Two representations of 0
21
Negatives: Two ’ s complement With N bit numbers, to compute negative –Invert all the bits –Add 1 Example: -25 in 8-bit two ’ s complement – 25 00011001 – Invert bits: 11100110 – Add 1: 1 11100111
22
2 ’ s Complement: Pros and Cons Con: –Not so easy to comprehend –Human must convert negative to identify Pro: –Addition is exactly same as for positives No additional hardware for negatives, and subtraction. –One representation of 0
23
2 ’ s Complement: Examples Compute negative of -25 (8-bits) – We found -25 to be 11100111 – Invert bits: 00011000 – Add 1: 00011001 – Recognize this as 25 in binary Add -25 and 37 (8-bits) – 11100111 (-25) + 00100101 ( 37) (1)00001100 – Recognize as 12
24
Facts about 2 ’ s Complement Leftmost bit still tells whether number is positive or negative as with sign-magnitude 2 ’ s complement is same as sign magnitude for positives
25
2 ’ s complement to decimal (examples) Assume 8-bit 2 ’ s complement: – X = 11011001 -X = 00100110 + 1 = 00100111 = 32+4+2+1 = 39 (decimal) So, X = -39 – X = 01011001 Since X is positive, we have X = 64+16+8+1 = 89
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.