Transcript Document
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 1 Representing Numbers: Integers • • • weighting: Digits 1649 = 103 1 1*103 + 102 6 6*102 + 101 4 4*101 + 100 9 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 2 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 Comp 1001: IT & Architecture - Joe Carthy Octal: 111O 3 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 bits 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 26 1 25 0 24 1 23 1 Comp 1001: IT & Architecture - Joe Carthy 22 1 21 0 20 0 4 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 5 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 6 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: 35 / 2 17 / 2 8/2 4/2 2/2 1/2 0 Remainder 1 1 0 0 0 1 The result is read upwards giving 3510 = 1000112. Comp 1001: IT & Architecture - Joe Carthy 7 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, 5 32 is 2 , 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 8 Representing Numbers: Converting Decimal to Binary • Binary Numbers occur so frequently that you should remember Binary Decimal 111 7 1111 15 0111 1111 127 1111 1111 255 Comp 1001: IT & Architecture - Joe Carthy because they 9 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 10 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: digits 2FA = = = = 162 2 2 * 162 + 2 * 162 + 256 + 50610 161 F F * 161 15 * 161 240 Comp 1001: IT & Architecture - Joe Carthy + + + 160 A A * 160 10 * 160 10 11 Hexadecimal Number System Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Comp 1001: IT & Architecture - Joe Carthy 12 Hexadecimal Number System • Example 2: Convert FFFH to decimal weighting: digits 2FA • • • = = = = 162 F F * 162 + 15 * 162 + 3840 + 409510 161 F F * 161 15 * 161 240 + + + 160 F F * 160 15 * 160 15 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 13 Hexadecimal Number System • Hexadecimal to Binary • Convert each Hex digit to a 4-bit binary number • Example: 7FAH 7 0111 F 1111 A 1010 7FA = 0111 1111 1010B • Exercise: Convert FFH, FEH, BBH to binary Comp 1001: IT & Architecture - Joe Carthy 14 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 Decimal Hex 1111 15 F 0011 3 3 1011 11 B 111100111011B = F3BH • Exercise: Convert 11 1111 1010B; 111 1101 1111B and 1111 1111 1111 1111B to Hexadecimal Comp 1001: IT & Architecture - Joe Carthy 15 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 16 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 17 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 18 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 Weights -128 +64 +32 +16 +8 Value = -128 + 2*1 + 1*1 = -128 +3 = -125D 0 +4 1 +2 1 +1 So 1000 0011B = -125D Exercise: Convert the 2’s complement numbers 1000 0111B and 1000 1111B to decimal. Comp 1001: IT & Architecture - Joe Carthy 19 Signed Numbers: 2’s Complement • Example 2: The 2’s complement number 1111 1111B Bits 1 1 1 1 Weights -128 +64 +32 +16 Value = -128 + 64 + 32 +16 + 8 +4 + 2 _+1 = -128 +127 = -1D 1 +8 1 +4 1 +2 1 +1 • 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 20 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 21 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 (complement of 1 is 0; complement of 0 is 1) to 0’s and change all 0’s to 1’s • 1111 1111B => 0000 0000B • Step 2: Add 1 • 0000 0000B + 1B => 0000 0001B => 1D Remember the sign bit was 1 => negative • So 1111 1111B => -1D Comp 1001: IT & Architecture - Joe Carthy 22 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 23 • 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. Bit Patte rn 0000 0000 0000 0001 0000 0010 Unsigne d 0 1 2 0111 1110 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 126 127 128 129 254 255 2's C om ple m e nt 0 1 2 126 127 -128 -127 -2 -1 Comp 1001: IT & Architecture - Joe Carthy 24 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 25 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 26 Number of bits in Memory Address • The maximum amount of memory that a processor can access by 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 limit on 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 4 Gb (232 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 is determined 27 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 28 Overflow • What happens if we attempt to store a larger unsigned value than 255 in 8-bits? • For example, if we attempt the calculation 70 + 75 using 8-bit two’s complement numbers, the result of 145 (1001 0001B) is a negative number in 2's complement! • This situation, when it arises is called overflow. It occurs when we attempt to represent a number outside the range of numbers that can be stored in a number of bits. • Overflow is detected by the hardware of the CPU. • This allows the programmer to test for this condition in an assembly language program and deal with it appropriately. Comp 1001: IT & Architecture - Joe Carthy 29 Overflow • The figure below illustrates the relationship between number range and overflow for 8-bit two’s complement numbers. Nu m be r Ran ge O ve rfl ow Re gi on -128 0 Comp 1001: IT & Architecture - Joe Carthy O ve rfl ow Re gi on 127 30 Exercises • Convert the following two’s complement numbers to decimal: FFFFh ; F000h; 1000h; 1001h • What is the range of unsigned numbers that can be represented by 20-bit, 24-bit and 32-bit numbers? • What is the range of numbers that can be represented using 32-bit two’s complement numbers? • What problem arises in representing zero in signed magnitude? • What is overflow and how might it occur? Comp 1001: IT & Architecture - Joe Carthy 31 Note: What does a bit-pattern represent ? • How can we tell by looking at a number whether it is a two’s complement number or an unsigned number. Does 1111 1111B represent the decimal number 255 or the number -1? • The answer is that we cannot tell by looking at a number, how it should be interpreted. • It is the responsibility of the programmer to use the number correctly. It is important to remember that you can never tell how any byte or longer bit-pattern is to be interpreted by looking at its value alone. It could represent a signed or unsigned number, an ASCII code, a machine code instruction and so on. The context (in which the information stored in the byte is used) will determine how it is to be interpreted. • Assembly languages provide separate instructions for handling comparisons involving unsigned or signed numbers. It is the programmers responsibility to use the correct instructions. Comp 1001: IT & Architecture - Joe Carthy 32 Real Numbers: Floating Point Numbers • • A different representation is used for real (usually called floating-point) numbers. We can write such numbers in scientific notation • Example: The number 562.42 can be written as 0.56242 x 103. • We can express any floating-point number as: ± m x rexp where m is the mantissa, r is the radix and exp is the exponent. • For decimal numbers the radix is 10 and for binary numbers the radix is 2. • Since we use binary numbers in a computer system, we do not have to store the radix explicitly when representing floating-point numbers. This means that we only need to store the mantissa and the exponent of the number to be represented. Comp 1001: IT & Architecture - Joe Carthy 33 Real Numbers: Floating Point Numbers • For the number 0.11011011 x 23 only the values 11011011 (mantissa) and 3 (exponent converted to binary) need to be stored. The binary point and radix are implicit. • A floating-point number is normalised if the most significant digit of the mantissa is non-zero as in the above example. Any floating-point number can be normalised by adjusting the exponent appropriately. For example, 0.0001011 is normalised to 0.1011 x 2-3. • To represent 0, as a floating-point number, both the mantissa and the exponent are represented as zero. Comp 1001: IT & Architecture - Joe Carthy 34 Real Numbers: Floating Point Numbers • There are various standards (IEEE, ANSI etc.) that define how the mantissa and exponent of a floating-point number should be stored. • Most standards use a 32-bit format for storing single precision floating-point numbers and a • 64-bit format for storing double precision floating-point numbers. • A possible format for a 32-bit floating-point number uses a sign bit, 23 bits to represent the mantissa and the remaining 8 bits to represent the exponent. • The mantissa could be represented using either signed magnitude or 2’s complement. The exponent could be in 2’s complement (but another form called excess notation is also used). Thus a 32-bit floating-point number could be represented as follows, where S is the sign bit: . Comp 1001: IT & Architecture - Joe Carthy 35 Real Numbers: Floating Point Numbers General Format and example: 0.1101 1011 x 26 1 bit 8 bits 23 bits S Exponent sign exponent Mantissa mantissa 0 0000 0110 000 0000 0000 0000 1101 1011 Comp 1001: IT & Architecture - Joe Carthy 36 Number Representation in Java Keyword Description Size/Format Byte-length integer Short integer Integer Long integer 8-bit two's complement 16-bit two's complement 32-bit two's complement 64-bit two's complement • Integers • byte • short • int • long • Real numbers • float • double Single-precision floating point Double-precision floating point 32-bit IEEE 754 64-bit IEEE 754 • Other types • char • boolean A single character A boolean value (true or false) 16-bit Unicode character true or false . Comp 1001: IT & Architecture - Joe Carthy 37 Number Representation in Java Range • Integers • • • byte short int -128 to 127 -32,768 to 32,767 -2,147,483,648 to 2,147,483,467 -(27) to 27-1 -(215) to 215-1 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 -(264) to 264-1 • long • • Real numbers float +/- 1.40239846 * 2-45 to +/- 3.40282347 * 238 double +/- 4.94065645841246544 * 2-324 to +/- 1.79769313486231570 * 2308 • -(232) to 232-1 . Comp 1001: IT & Architecture - Joe Carthy 38 Why use small variables (byte, short) ? • Programmers use “small” variable types to save memory usage • Your program occupies memory space. Each variable used occupies one or more storage locations. By using byte and short you decrease the amount of RAM required But modern PCs have hundreds of megabytes of RAM - so who cares about the few bytes you can save ? For PC applications these savings are usually of no consequence BUT most processors are NOT used in computers ! They are embedded in other equipment: phones, stereos, washing machines, microwaves etc. Usually these processors have very limited amounts of RAM. Called micro-controllers This is where it is important to write programs that are as small as possible and so to use the most efficient variable types. • • • • • • • • . Comp 1001: IT & Architecture - Joe Carthy 39 Review • • Floating point numbers are represented in scientific notation In binary: ± m x 2exp • There are different standards for representing floating point numbers • There are different precisions: single and double • Languages like Java and C allow you specify the type of number • Micro-controllers are embedded processors with some RAM • Code for micro-controller needs to be designed to use as little RAM as possible. Comp 1001: IT & Architecture - Joe Carthy 40