CMPE12 – Notes chapter 3 Integers (Textbook Chapter 2) Positional Number System (again) • Select a number as the base, b, ex: • Define an alphabet of b-1 symbols plus a symbol for zero to represent all numbers… • Use an ordered sequence of 2 or more digits to represent numbers… • The represented number is the sum of all digits, each multiplied by b to the power of the digit’s position p CMPE12 – Winter 2011 – J. Ferguson 3-2 Base conversion Three cases: I. From any base b to base 10 II. From base 10 to any base b III. From any base b to any other base c CMPE12 – Winter 2011 – J. Ferguson 3-3 I. From base b to base 10 Use sum of products: n-1 Value = i=0 Σ (di b i) • Ex: 123410 = 123410 • Ex: 12345 = (625+250+75+4)10 = 95910 • Ex: 1001012 = (32+4+1)10 = 3710 CMPE12 – Winter 2011 – J. Ferguson 3-4 II. From base 10 to base b • Use successive divisions. Divide by the base and the remainder is the least significant digit that is not yet i N q r found Trivial example: 436710 = ?10 CMPE12 – Winter 2011 – J. Ferguson 0 4367 436 7 1 2 436 43 43 4 6 3 3 4 0 4 3-5 From base 10 to base b • A nontrivial example. Ex: 12310 to base 3 CMPE12 – Winter 2011 – J. Ferguson i N q r 0 1 2 3 4 5 6 7 123 41 13 4 1 41 13 4 1 0 0 2 1 1 1 3-6 III. From base b to base c • The easiest way is to convert from base b to base 10 first, and then from 10 to c • Or, in some cases, it’s easier to use base 2 as the intermediate base (we’ll see them soon) CMPE12 – Winter 2011 – J. Ferguson 3-7 Positional Addition Examples: CMPE12 – Winter 2011 – J. Ferguson 3-8 Positional multiplication 1135 *425 --------------------------- Not as hard as it looks. CMPE12 – Winter 2011 – J. Ferguson 3-9 Numbers for computers • There are many ways to “represent” a number • Representation does not affect computation result • Representation affects difficulty of computing results • Computers need a representation that works with (fast) electronic circuits CMPE12 – Winter 2011 – J. Ferguson 3 - 10 Binary Number System • Base (radix): 2 • Alphabet: 0, 1 • Binary Digits, or bits • 10012 = • 110002 = CMPE12 – Winter 2011 – J. Ferguson 3 - 11 Octal Number System • Base (radix): 8 • Alphabet: 0 – 7 • 3458 = 3*64+4*8+5 = 22910 • 10018 = 51310 • In C, octal numbers are represented with a leading 0. Ex: CMPE12 – Winter 2011 – J. Ferguson 3 - 12 Hexadecimal Number System • Base (radix): 16 • Alphabet: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F Hex Decimal A 10 B 11 C 12 D 13 E 14 F 15 • In C: leading “0x” (e. g. 0xa3) • In LC-3: leading “x” (for example, “x3000”) CMPE12 – Winter 2011 – J. Ferguson 3 - 13 Examples of converting hex numbers to decimal A316 = 3E816 = CMPE12 – Winter 2011 – J. Ferguson 3 - 14 Decimal -> Binary Conversion • Method 1: • Divide decimal value by 2 until the value is 0 • Ex: CMPE12 – Winter 2011 – J. Ferguson 3 - 15 Decimal -> Binary Conversion • Method 2: • Know your powers of two and subtract • Ex: 61 20 1 21 2 22 4 23 8 24 16 25 32 26 64 27 128 28 256 29 512 210 1024 • What is the biggest power of two that fits? • What is the remainder? • What fits? • What is the remainder? • … • What is the binary representation? CMPE12 – Winter 2011 – J. Ferguson 3 - 16 Binary -> Octal Conversion • Group into triplets starting at least significant bit • Add leading 0’s if needed (why not trailing?) • Write 1 octal digit for each group of three binary digits • Examples: 100 010 111 (binary) (octal) 10 101 110 (binary) (octal) CMPE12 – Winter 2011 – J. Ferguson 3 - 17 Octal -> Binary Conversion It is simple, just write down the 3-bit binary code for each octal digit CMPE12 – Winter 2011 – J. Ferguson Octal Binary 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 3 - 18 Binary -> Hex Conversion • Group into 4’s starting at least significant bit • Add leading 0’s if needed • Write 1 hex digit for each group • Ex: 0x 0x 1001 1110 0111 00002 = 0001 1111 1010 00112 = CMPE12 – Winter 2011 – J. Ferguson 3 - 19 Hex -> Binary Conversion Again, simply write down the 4-bit binary code for each hex digit Ex: 0x 3 9 c 8 = 2 CMPE12 – Winter 2011 – J. Ferguson 3 - 20 Conversion Table Decimal Hexadecimal Octal Binary 0 0 0 0000 1 1 1 0001 2 2 2 0010 3 3 3 0011 4 4 4 0100 5 5 5 0101 6 6 6 0110 7 7 7 0111 8 8 10 1000 9 9 11 1001 10 A 12 1010 11 B 13 1011 12 C 14 1100 13 D 15 1101 14 E 16 1110 15 F 17 1111 CMPE12 – Winter 2011 – J. Ferguson 3 - 21 Hex and Octal can be used to represent Binary! Because A53216 is less error prone and is easier to write than 10100101001100102 ! So when I am talking about data in a register or a memory address as a base 16 number it really represents a base 2 number. CMPE12 – Winter 2011 – J. Ferguson 3 - 22 Largest number What is M, the largest number that we can represent on n digits • In base 10? • In base 2? • In octal? • In hex? • In base 7? • In base b? How many different numbers can we represent with n digits in base b? CMPE12 – Winter 2011 – J. Ferguson 3 - 23 Binary addition examples CMPE12 – Winter 2011 – J. Ferguson 3 - 24 Unsigned binary subtraction Same principle as in decimal (or any base b): ripple borrow. Ex: CMPE12 – Winter 2011 – J. Ferguson 3 - 25 Unsigned binary multiplication Same principle as in decimal (or any base b): sum of partial products. Ex: CMPE12 – Winter 2011 – J. Ferguson 3 - 26 What about Negative Numbers? Until now we have been talking about “unsigned” integers. CMPE12 – Winter 2011 – J. Ferguson 3 - 27 Signed numbers: in “human” • Most humans precede number with “-” (e.g., -2000) • Accountants, however, use parentheses: (2000) or color 2000 • Called Sign-and-magnitude format • Example: -100010 in hex? 100010 = 3 x 162 + e x 161 + 8 x 160 = -3E816 If there is no sign, it is assumed positive CMPE12 – Winter 2011 – J. Ferguson 3 - 28 Signed numbers: in binary Problem: There is no voltage specifically for - Four main choices: • • • • Sign-and-magnitude One’s complement Two’s complement Biased notation CMPE12 – Winter 2011 – J. Ferguson 3 - 29 Sign-and-magnitude (without a minus sign) • How does it work? x y 000 001 • Advantages – Intuitive – Easy to invert the sign • Disadvantages: – Add and sub must compare sign and magnitude of the operands – Two representations of zero CMPE12 – Winter 2011 – J. Ferguson 010 011 100 101 110 111 3 - 30 Sign-and-magnitude addition and subtraction • Depends on sign of operands: – Same sign: add the magnitudes, keep the sign – Different: subtract smaller from larger, keep sign of larger • Ex: A+B, with A = 6, B = -10, on 5 bits Complicated and slow CMPE12 – Winter 2011 – J. Ferguson 3 - 31 CMPE12 – Winter 2011 – J. Ferguson 3 - 32 Sign-and-magnitude mapping Number represented Number 000 encoded 001 010 011 100 101 110 111 Range on n bits: from -2n-1-1 to 2n-1-1 CMPE12 – Winter 2011 – J. Ferguson 3 - 33 One’s complement • How does it work? x y 000 001 010 • Advantages – Still intuitive… – Easy to invert the sign • Disadvantages: - Add and sub require a special operation (end-around carry) – Two representations of zero CMPE12 – Winter 2011 – J. Ferguson 011 100 101 110 111 3 - 34 One’s complement addition and subtraction Add/subtract normally, add 1 if carry-out • Ex: A+B, with A = 3, B = 2, on 4 bits. • Ex: A+B, with A = -3, B = -2. • Ex: A+B, with A = -3, B = 2. CMPE12 – Winter 2011 – J. Ferguson 3 - 35 One’s complement mapping Number represented Number 000 encoded 001 010 011 100 101 110 111 Range on n bits: CMPE12 – Winter 2011 – J. Ferguson 3 - 36 Two’s complement • How does it work? x y 000 001 • Advantages – Add/sub are simple (and easy in hw) – Subtraction only requires one adder – Single zero • Disadvantages: – Asymmetric range – Not intuitive at first for humans. 010 011 100 101 110 111 This is what today’s computers use! CMPE12 – Winter 2011 – J. Ferguson 3 - 37 Finding the Additive Inverse in 2’s complement notation • Often called “taking the 2’s Complement” • Process is used to – Convert an unsigned integer to a negative 2’s complement number. – Find the absolute value of a negative 2’s complement number – First step to convert to signed decimal from a negative 2’s complement integer. CMPE12 – Winter 2011 – J. Ferguson 3 - 38 Finding the Additive Inverse in 2’s complement notation • Method 1: “flip the bits and add one” • Method 2: “keep all the leftmost zeros and the first one, flip all other bits” CMPE12 – Winter 2011 – J. Ferguson 3 - 39 Finding the Additive Inverse in 2’s complement notation • Method 2: “keep all the leftmost zeros and the first one, flip all other bits” CMPE12 – Winter 2011 – J. Ferguson 3 - 40 More Examples CMPE12 – Winter 2011 – J. Ferguson 3 - 41 Converting from Two’s Complement to Decimal • If Positive? • If Negative? 1. Find its additive inverse (sometimes called “taking the two’s complement of”). 2. Convert to Decimal 3. Insert negative sign. CMPE12 – Winter 2011 – J. Ferguson 3 - 42 Examples CMPE12 – Winter 2011 – J. Ferguson 3 - 43 Addition and subtraction in two’s complement Addition: normal binary addition • Ex: A+B, with A = 2, B = 1, on 3 bits Subtraction: convert to adding the negative (two’s complement) • Ex: A-B, with A = 1, B = 2, on 3 bits CMPE12 – Winter 2011 – J. Ferguson 3 - 44 More Examples CMPE12 – Winter 2011 – J. Ferguson 3 - 45 Overflow • Numbers are stored in some form of memory (or registers) and the memory is organized with limits on the number of bits available to represent the number. • This leads to the issue of what to do when you perform an operation and your answer is outside of the range that can be represented by the number of bits available. • In a computer this is called overflow. • Overflow depends on the representation’s precision (number of bits). ( Next slide for example) CMPE12 – Winter 2011 – J. Ferguson 3 - 46 Overflow Continued • Ex: unsigned binary integer addition to be stored in a 32 bit register: A32616+900016 All computer systems have a procedure to deal with overflow. CMPE12 – Winter 2011 – J. Ferguson 3 - 47 2’s Complement: Overflow • Overflow detection is easy! When adding two operands of the same sign and the sign of the sum must be the same! Ex: 0100 + 0101 with 4 bits of precision: • Ex: 1010 + 1001 with 4 bits: • NOTE: in two’s complement, there can’t be overflow with operands of different sign. Why? CMPE12 – Winter 2011 – J. Ferguson 3 - 48 Representation is too small for the register Unsigned Integers: how do you put 1010 into a 16 bit register? 2’s Complement: Sign-Extension. “Extend” the sign bit out to the end of the register CMPE12 – Winter 2011 – J. Ferguson 3 - 49 Examples CMPE12 – Winter 2011 – J. Ferguson 3 - 50 Two’s complement mapping -2 1 2 3 -4 Number 000 encoded 001 010 011 100 101 110 100 101 110 111 000 001 010 011 -3 -2 -1 0 1 2 3 Number represented 0 -4 -3 -1 111 Range on n bits:-2n-1 < x < 2n-1-1 CMPE12 – Winter 2011 – J. Ferguson 3 - 51 Integer data types • Byte: ordered set of 8 bits • Nybble: ordered set of 4 bits • Word: ordered set of… CMPE12 – Winter 2011 – J. Ferguson 3 - 52 Character Data type 7-bit ASCII (American Standard Code for Information Interchange) table CMPE12 – Winter 2011 – J. Ferguson 3 - 53 Exercises due Friday January 28 • • • • Ex 2.1 to 2.8 Ex 2.9 to 2.15, 2.17 Ex 2.28, 2.30, 2.34 Ex 2.37, 2,38 CMPE12 – Winter 2011 – J. Ferguson 3 - 54
© Copyright 2026 Paperzz