# Representing Numbers: Integers

## Presentation on theme: "Representing Numbers: Integers"— Presentation transcript:

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* * * *100 Comp 1001: IT & Architecture - Joe Carthy

Representing Numbers: Integers
weighting: Digits 1649 = 1* * * *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 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: Binary:1112 Hex: 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 Weight bits = 0*27 + 1*26 + 0*25 + 1*24 + 1*23 + 1*22 + 0*21 + 0*20 = = 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 = = 15 Comp 1001: IT & Architecture - Joe Carthy

Representing Numbers: Binary Numbers
Exercises Convert the following binary numbers to decimal: (i) (ii) (iii) (iv) (v) (vi) 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 The result is read upwards giving 3510 = 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 ; 128 is 27 so we write it as 1 followed by 7 zeros, i.e 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 they occur so frequently Binary Decimal 111 7 1111 15 127 255 Comp 1001: IT & Architecture - Joe Carthy

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 B, B and B to decimal Comp 1001: IT & Architecture - Joe Carthy

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 F A 2FA = 2 * F * A * 160 = 2 * * * 160 = = 50610 Comp 1001: IT & Architecture - Joe Carthy

Comp 1001: IT & Architecture - Joe Carthy

Example 2: Convert FFFH to decimal weighting: digits F F F 2FA = F * F * F * 160 = 15 * * * 160 = = 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 to Binary Convert each Hex digit to a 4-bit binary number Example: 7FAH 7 F A 7FA = B Exercise: Convert FFH, FEH, BBH to binary Comp 1001: IT & Architecture - Joe Carthy

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: B Decimal Hex F B B = F3BH Exercise: Convert B; B and B to Hexadecimal Comp 1001: IT & Architecture - Joe Carthy

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 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

Comp 1001: IT & Architecture - Joe Carthy
Signed Numbers Example +15 and -15 as 8-bit numbers +15 => B MSB = 0 => + -15 => B MSB = 1 => - Note the magnitude is comprised of 7 bits Largest positive number is => +127 Largest negative number is => -127 Two representations of zero ! and 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: 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 B is weighted as follows Bits Weights Value = *1 + 1*1 = = -125D So B = -125D Exercise: Convert the 2’s complement numbers B and B to decimal. Comp 1001: IT & Architecture - Joe Carthy

Signed Numbers: 2’s Complement
Example 2: The 2’s complement number B Bits Weights Value = _+1 = = -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: = 0 In 2’s complement: B B B 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 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 B 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) B => B Step 2: Add 1 B + 1B => B => 1D Remember the sign bit was 1 => negative So B => -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 => B Step 1: Flip the bits B => B Step 2: Add 1 B + 1B => B => -1D => B Exercise: Convert the following 2’s complement numbers to decimal: ; ; ; Decimal to 2’s complement: -128 ; -65; -2 Comp 1001: IT & Architecture - Joe Carthy

Comp 1001: IT & Architecture - Joe Carthy
B (i.e. -128) is the largest negative 8-bit 2’s complement number (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 ) and two’s complement numbers in the range to (-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