280 likes | 397 Views
Representing Numbers: Integers. Humans use Decimal Number System Computers use Binary Number System Important to understand Decimal system before looking at binary system Decimal Numbers - Base 10 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
E N D
Representing Numbers: Integers • Humans use Decimal Number System • Computers use Binary Number System • Important to understand Decimal system before looking at binary system • Decimal Numbers - Base 10 • 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 • Positional number system: the position of a digit in a number determines its value • Take the number 1649 • The 1 is worth 1000 • The 9 is worth 9 units • Formally, the digits in a decimal number are weighted by increasing powers of 10 i.e. they use the base 10. We can write 1649 in the following form: • 1*103 + 6*102 + 4*101 + 9*100 Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Integers • weighting: 103 102 101 100 • Digits 1 6 4 9 • 1649 = 1*103 + 6*102 + 4*101 + 9*100 • Least Significant Digit: rightmost one - 9 above • Lowest power of 10 weighting • Digits on the right hand side are called the low-order digits (lower powers of 10). • Most Significant Digit: leftmost one - 1 above • Highest power of 10 weighting • The digits on the left hand side are called the high-order digits (higher powers of 10) Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Decimal Numbers • Largest n-digit number ? • Made up of n consecutive 9’s (= 10n-1 ) • Largest 4-digit number if 9999 • 9999 is 104-1 • Distinguishing Decimal from other number systems such as Binary, Hexadecimal (base 16) and Octal (base 8) • How do we know whether the number 111 is decimal or binary • One convention is to use subscripts • Decimal: 11110 Binary:1112 Hex: 11116 Octal: 1118 • Difficult to write use keyboard • Another convention is to append a letter (D, B, H, O) • Decimal: 111D Binary:111B Hex: 111H Octal: 111O Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Binary Numbers • Binary numbers are Base 2 numbers • Only 2 digits: 0 and 1 • Formally, the digits in a binary number are weighted by increasing powers of 2 • They operate as decimal numbers do in all other respects • Consider the binary number 0101 1100 • Weight 27 26 25 24 23 22 21 20 • bits 0 1 0 1 1 1 0 0 • 01011100 = 0*27 + 1*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 0*20 • = 0 + 6410 + 0 + 1610 + 810 + 410 + 0 + 0 • = 9210 Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Binary Numbers • Leftmost bit is the most significant bit (MSB). • The leftmost bits in a binary number are referred to as the high-order bits. • Rightmost bit is the least significant bit (LSB). • The rightmost bits in a binary number are referred to as the low-order bits. • Largest n-bit binary number ? • Made up of n consecutive 1’s (= 2n -1) • e.g. largest 4-bit number: 1111 = 24 -1 = 15 Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Binary Numbers • Exercises • Convert the following binary numbers to decimal: • (i) 1000 1000 (ii) 1000 1001 (iii) 1000 0111 • (iv) 0100 0001 (v) 0111 1111 (vi) 0110 0001 • Joe Carthy Formatting Convention • In these notes we insert a space after every 4 bits to make the numbers easier to read Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Converting Decimal to Binary • To convert from one number base to another: • you repeatedly divide the number to be converted by the new base • the remainder of the division at each stage becomes a digit in the new base • until the result of the division is 0. • Example: To convert decimal 35 to binary we do the following: • Remainder • 35 / 2 1 • 17 / 2 1 • 8 / 2 0 • 4 / 2 0 • 2 / 2 0 • 1 / 2 1 • 0 • The result is read upwards giving 3510 = 1000112. Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Converting Decimal to Binary • Exercise: Convert the following decimal numbers to binary • (1) 64 (2) 65 (3) 32 (4) 16 (5) 48 • Shortcuts • To convert any decimal number which is a power of 2, to binary, simply write 1 followed by the number of zeros given by the power of 2. • For example, 32 is 25, so we write it as 1 followed by 5 zeros, i.e. 10000; 128 is 27 so we write it as 1 followed by 7 zeros, i.e. 100 0000. • Remember that the largest binary number that can be stored in a given number of bits is made up of n 1’s. • An easy way to convert this to decimal, is to note that this is the same as 2n - 1. • For example, if we are using 4-bit numbers, the largest value we can represent is 1111 which is 24-1, i.e. 15 Comp 1001: IT & Architecture - Joe Carthy
Representing Numbers: Converting Decimal to Binary • Binary Numbers that you should remember because theyoccur so frequently Comp 1001: IT & Architecture - Joe Carthy
Review • Decimal Number System - Base 10 • Significant Digits • Binary Number System - Base 2 • Notation (B,D,H,O) • Binary to Decimal • Decimal to Binary • Shortcuts and Common Binary Numbers • Review Questions • What is a positional number system ? • What is the MSB and the LSB. Give an example of each one. • Show how the weights of the bits in an • 8-bit binary number • 16-bit binary numbe • What is the weight of the MSB in (a) 8-bit number (b) 16-bit number (c) 32-bit number • Convert 48D, 65D, 31D, 15D to binary • Convert 1111 0111B, 1010 1010B and 1110 0111B to decimal Comp 1001: IT & Architecture - Joe Carthy
Hexadecimal Number System • Base-16 number System • 16 digits: 0, 1, 2, .., 9, A, B, C, D, E, F. • We use the letters A to F to represent numbers 10 to 15 using a single symbol • A = 10; B = 11; C = 12; D = 13; E = 14; F = 15; • Use H at right hand side to indicate Hexadecimal • Used because • binary numbers are very long and so are error prone • Easy to convert between hexadecimal and binary than between decimal and binary • Example: Convert 2FAH to decimal weighting: 162 161 160 digits 2 F A 2FA = 2 * 162 + F * 161 + A * 160 = 2 * 162 + 15 * 161 + 10 * 160 = 256 + 240 + 10 = 50610 Comp 1001: IT & Architecture - Joe Carthy
Hexadecimal Number System Comp 1001: IT & Architecture - Joe Carthy
Hexadecimal Number System • Example 2: Convert FFFH to decimal weighting: 162 161 160 digits F F F 2FA = F * 162 + F * 161 + F * 160 = 15 * 162 + 15 * 161 + 15 * 160 = 3840 + 240 + 15 = 409510 • Example 3: Convert 65D to Hexadecimal • 65 / 16 = 4 Remainder 1 • 4 / 16 = 0 Remainder 4 • 65D = 41H • Exercise: Convert 97D, 48D, 255D to Hexadecimal Comp 1001: IT & Architecture - Joe Carthy
Hexadecimal Number System • Hexadecimal to Binary • Convert each Hex digit to a 4-bit binary number • Example: 7FAH 7 F A 0111 1111 1010 7FA = 0111 1111 1010B • Exercise: Convert FFH, FEH, BBH to binary Comp 1001: IT & Architecture - Joe Carthy
Hexadecimal Number System • Binary to Hexadecimal • Break binary numbers into groups of 4-bits from right hand side • Pad with 0’s on left if necessary • Convert each group of 4-bits to its equivalent Hex digit • Example 1: 111100111011B 1111 0011 1011 Decimal 15 3 11 Hex F 3 B 111100111011B = F3BH • Exercise: Convert 11 1111 1010B; 111 1101 1111B and 1111 1111 1111 1111B to Hexadecimal Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers • How do we represent negative numbers ? • Humans use a symbol to indicate number sign: “-” or “+” • In computer we only have binary: 1’s and 0’s . • Two common methods for representing signed numbers • Signed Magnitude • Two’s Complement (2’s Complement) • In the following assume we are working with 8-bit numbers • Signed Magnitude • We designate the leftmost bit i.e the MSB as a sign bit • The sign bit indicates whether a number is positive or negative • 0 sign bit => positive number • 1 sign bit => negative number • The remaining bits give the magnitude of the number Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers • Example +15 and -15 as 8-bit numbers • +15 => 0 000 1111B MSB = 0 => + • -15 => 1 000 1111B MSB = 1 => - • Note the magnitude is comprised of 7 bits • Largest positive number is 0111 1111 => +127 • Largest negative number is 1 111 111 => -127 • Two representations of zero ! 0 000 0000 and 1 000 0000 Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • In a complementary number system each number has a unique representation. • Two’s complement is a complementary number system used in computers • It is the most commonly used method for representing signed numbers • Uses only one representation of zero:0000 0000 • Uses a sign bit as for signed magnitude • 0 sign bit => positive number • 1 sign bit => negative number • In the case of positive numbers, the representation is identical to that of signed magnitude, • the sign bit is 0 and the remaining bits represent the positive number. • In the case of negative numbers, the sign bit is 1 but the bits to the right of the sign bit do not directly indicate the magnitude of the number. Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • In negative numbers the sign bit carries a negative weight while all other bits carry a positive weight e.g. the 2’s complement number 1000 0011B is weighted as follows Bits 1 0 0 0 0 0 1 1 Weights -128 +64 +32 +16 +8 +4 +2 +1 Value = -128 + 2*1 + 1*1 = -128 +3 = -125D So 1000 0011B = -125D Exercise: Convert the 2’s complement numbers 1000 0111B and 1000 1111B to decimal. Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • Example 2: The 2’s complement number 1111 1111B Bits 1 1 1 1 1 1 1 1 Weights -128 +64 +32 +16 +8 +4 +2 +1 Value = -128 + 64 + 32 +16 + 8 +4 + 2 _+1 = -128 +127 = -1D • In 2’s complement each number has a unique representation i.e. the negative representation of a number uses a completely different bit pattern than its positive counterpart. • In any number system: +x - x = 0 Example: +1 - 1 = 0 In 2’s complement: 0000 0001B 1111 1111B ------------------ 0000 0000B Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • 2’s Complement Arithmetic • In 2’s Complement, we do not need subtraction to compute x - y • We simply add -y to x to get the result. • This makes it easier to design the hardware to implement 2’s arithmetic • It much more complicated with signed magnitude e.g to compute +2 - 6 we must always subtract the smaller number from the larger one and then take the sign of the larger number. • Quick Conversion to/from 2’s Complement • Use the rule: Flip the bits and Add 1 Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • Quick Conversion to/from 2’s Complement • Use the rule: Flip the bits and Add 1 • Example 1 Convert 2’s complement number 1111 1111B to decimal • Step 1: Flip the bits: Change 1’s to 0’s and change all 0’s to 1’s(complement of 1 is 0; complement of 0 is 1) • 1111 1111B => 0000 0000B • Step 2: Add 1 • 0000 0000B + 1B =>0000 0001B => 1DRemember the sign bit was 1 => negative • So 1111 1111B => -1D Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers: 2’s Complement • Example 2: Convert -1D to 2’s complement • First convert 1D to binary => 0000 0001B • Step 1: Flip the bits • 0000 0001B => 1111 1110B • Step 2: Add 1 • 1111 1110B + 1B =>1111 1111B => -1D => 1111 1111B • Exercise: Convert the following • 2’s complement numbers to decimal: 1111 1110; 1000 0000; 100 0001; 1111 0000 • Decimal to 2’s complement: -128 ; -65; -2 Comp 1001: IT & Architecture - Joe Carthy
1000 0000B (i.e. -128) is the largest negative 8-bit 2’s complement number • 0111 1111 (127) is the largest positive 8-bit 2’s complement number • 256 numbers can be represented using 8-bit two’s complement numbers from -128 to 127. • There is only one representation for zero. • The table below lists the decimal equivalents of some 8-bit 2’s complement • and unsigned binary numbers. Comp 1001: IT & Architecture - Joe Carthy
Number Range and Overflow • The range of numbers (called the number range) that can be stored in a given number of bits is important. • Given an 8-bit number, we can represent • unsigned numbers in the range 0 to 255 (0 to 28-1) and • two’s complement numbers in the range -128 to +127 (-27 to 27). • Given a 16-bit number, we can represent • unsigned numbers in the range 0 to 65,535 (0 to 216 -1) and • two’s complement numbers in the range -32768 to 32767 (-215 to 215-1). • In general given an n-bit number, we can represent • unsigned numbers in the range 0 to 2n -1 and • two’s complement numbers in the range -2n-1 to 2n-1 -1 • Exercise: What is the number range of 4-bit, 10-bit, 20-bit, 30-bit and 32-bit numbers ? Comp 1001: IT & Architecture - Joe Carthy
Number Range and Overflow • The magnitude of an unsigned number doubles for every bit added • 10 bits can represent 1024 numbers (1K) • 11 bits => 2048 numbers (2K) • 12 bits => 4096 numbers (4K) • …. • 16 bits => 64K numbers • .. • 20 bits => 220 => 1 Mb • 21 bits => 2 Mb • .. • 24 bit => 16Mb • .. • 30 bits => 230 => 1 Gb • 31 bits => 2Gb • 32 bits => 4 Gb Comp 1001: IT & Architecture - Joe Carthy
Number of bits in Memory Address • The maximum amount of memory that a processor can access is determinedby the number of bits that the processor uses to represent a memory address. • This determines the maximum memory address that can be accessed i.e. is a limiton the maximum amount of RAM a computer can use • For example, a processor that uses 16-bit addresses will only be able to access • up to 65,536 memory locations (64Kb), with addresses from 0 to 65,535. • A 20-bit address allows up to 220 (1Mb) memory locations to be accessed • A 24-bit address allows up to 16Mb (224 bytes) of RAM to be accessed • A 30-bit address allows up to 1Gb (230 bytes) of RAM to be accessed • A 32-bit address allows up to 1Gb (230 bytes) of RAM to be accessed • Most PCs now use 32-bit addresses. Original PC (1981) used 20-bit addresses. • Early Macintoshs used 24-bit addresses. Comp 1001: IT & Architecture - Joe Carthy
Review • Hexadecimal: 16 digits: 0 to 9 and A to F. Easy to convert to binary • Signed Numbers: Signed Magnitude and 2’s Complement • Sign bit: MSB 1 => negative • 2 Complement: Flip the bits and add 1. • Number range is important • Given an n-bit number, we can represent • unsigned numbers in the range 0 to 2n -1 and • two’s complement numbers in the range -2n-1 to 2n-1 -1. • A 20-bit address allows up to 220 (1Mb) memory locations to be accessed • A 30-bit address allows up to 1Gb (230 bytes) of RAM to be accessed • A 32-bit address allows up to 1Gb (230 bytes) of RAM to be accessed Comp 1001: IT & Architecture - Joe Carthy