11.1 11.2 Unit 11 Signed Representation Systems BINARY REPRESENTATION SYSTEMS Binary Arithmetic REVIEW 11.3 11.4 Interpreting Binary Strings Binary Representation Systems • Codes • Given a string of 1’s and 0’s, you need to know the • Integer Systems – Text – Unsigned representation system being used, before you can • ASCII / Unicode • Unsigned (Normal) binary understand the value of those 1’s and 0’s. – Decimal Codes – Signed • Information (value) = Bits + Context (System) • Signed Magnitude • BCD (Binary Coded Decimal) / (8421 Code) • 2’s complement • Excess-N* 01000001 = ? • 1’s complement* Unsigned • Floating Point* ASCII Binary system BCD System system – For very large and small (fractional) numbers 65 10 ‘A’ ASCII 41 BCD * = Not fully covered in this class
11.5 11.6 Unsigned and Signed • Normal (unsigned) binary can only represent positive numbers – All place values are positive • To represent BOTH positive and negative Signed Magnitude numbers we must use the available binary 2’s Complement System codes differently, some for the positive values SIGNED SYSTEMS and others for the negative values – We call these signed representations 11.7 11.8 Signed Number Representation Signed numbers • 2 Primary Systems • All systems used to represent negative numbers split the – ______________________ possible binary combinations in 0000 – ______________________ (most widely used for 1111 0001 half (half for positive numbers / 1110 0010 integer representation) half for negative numbers) 1101 0011 • In both signed magnitude and 1100 0100 2’s complement, positive and 1011 0101 negative numbers are 1010 0110 1001 0111 separated using the MSB 1000 – _________ means negative – _________ means positive
11.9 11.10 Signed Magnitude System Signed Magnitude Examples • Use binary place values but now MSB represents the sign (1 if negative, 0 if positive) 1 1 0 1 = -5 +/- 4 2 1 4-bit Signed Bit Bit Bit Bit Magnitude 3 2 1 0 0 0 1 1 = +3 Notice that +3 in signed 4-bit 0 to 15 magnitude is the same Unsigned +/- 4 2 1 as in the unsigned 8 4 2 1 system 1 1 1 1 = -7 +/- 4 2 1 Bit Bit Bit Bit 4-bit Signed 3 2 1 0 -7 to +7 Magnitude 1 0 0 1 0 0 1 1 = -19 +/- 4 2 1 +/- 64 32 16 8 4 2 1 8-bit Signed 0 0 0 1 1 0 0 1 = +25 Magnitude Bit Bit Bit Bit Bit Bit Bit Bit 8-bit Signed 7 6 5 4 3 2 1 0 +/- 64 32 16 8 4 2 1 -127 to +127 Magnitude Important: Positive numbers have the same representation in signed +/- 64 32 16 8 4 2 1 magnitude as in normal unsigned binary 11.11 11.12 Signed Magnitude Range Disadvantages of Signed Magnitude • Given n bits… 1. Wastes a combination to represent _____ – MSB is sign 0000 = 1000 = ____ – Other n-1 bits = normal unsigned place values 2. Addition and subtraction algorithms for signed • Range with n-1 unsigned bits = [0 to 2 n-1 -1] magnitude are different than unsigned binary (we’d like them to be the same to use same HW) Range with n-bits of Signed Magnitude [ -(2 n-1 –1) to +(2 n-1 –1)] 4 6 - 6 - 4 Swap & - make res. negative
11.13 11.14 2’s Complement System 2’s Complement Examples • Normal binary place values except MSB has ____________________ 1 0 1 1 = -5 – MSB of 1 = ______ -8 4 2 1 4-bit 2’s complement 0 0 1 1 = +3 Notice that +3 in 2’s comp. is the same as Bit Bit Bit Bit -8 4 2 1 in the unsigned system 3 2 1 0 4-bit 0 to 15 1 1 1 1 = -1 Unsigned 8 4 2 1 -8 4 2 1 1 0 0 0 0 0 0 1 = -127 Bit Bit Bit Bit 4-bit 3 2 1 0 -8 to +7 2’s complement 8-bit -128 64 32 16 8 4 2 1 2’s complement 0 0 0 1 1 0 0 1 = +25 __ 4 2 1 Bit Bit Bit Bit Bit Bit Bit Bit -128 64 32 16 8 4 2 1 8-bit 7 6 5 4 3 2 1 0 -128 to +127 2’s complement Important: Positive numbers have the _______ representation in 2’s complement as in normal unsigned binary ___ 64 32 16 8 4 2 1 11.15 11.16 2’s Complement Range Comparison of Systems 0 +1 • Given n bits… Signed -7 -6 0000 +2 Mag. 1111 0001 – Max positive value = _____________ 0 -2 -1 +1 1110 0010 • Includes all n-1 positive place values -5 +2 +3 -3 1101 0011 – Max negative value = _________________ +3 2’s comp. -4 -4 +4 • Includes only the negative MSB place value 1100 +4 0100 -5 Range with n-bits of 2’s complement +5 -3 1011 -6 0101 +6 +5 -7 [ -2 n-1 to +2 n-1 –1] 1010 +7 0110 -2 -8 1001 0111 +6 1000 -1 +7 -0 – Side note – What decimal value is 111…11?
11.17 11.18 Unsigned and Signed Variables IMPORTANT NOTE • In C, ________ variables use unsigned binary (normal • All computer systems use the 2's complement power-of-2 place values) to represent numbers system to represent signed integers ! unsigned char x = 147; • So from now on, if we say an integer is signed , 1 0 0 1 0 0 1 1 = +147 we are actually saying it uses the 128 64 32 16 8 4 2 1 2's complement system unless otherwise • In C, signed variables use the _________________ specified (Neg. MSB weight) to represent numbers – We will not use "signed magnitude" unless char x = -109; explicitly indicated 1 0 0 1 0 0 1 1 = -109 -128 64 32 16 8 4 2 1 11.19 11.20 Zero and Sign Extension Zero and Sign Truncation • Extension is the process of increasing the number of bits used • Truncation is the process of decreasing the number of bits used to represent a number without changing its value to represent a number without changing its value Unsigned = Zero Extension (Always add leading ___’s): Unsigned = Zero Truncation (Remove leading 0’s): 111011 = ___111011 Decrease an 8-bit number to 6-bit number by truncating 0’s. Can’t 00111011 = 111011 Increase a 6-bit number to 8-bit remove a ‘1’ because value is changed number by _____ extending 2’s complement = Sign Extension (Replicate _____ bit): 2’s complement = Sign Truncation (Remove _______ of sign bit): 011010 = ___011010 pos. pos. 00011010 = ________ _____ bit is just repeated Any copies of the MSB can be as many times as removed without changing the necessary neg. 110011 = ___110011 numbers value. Be careful not to neg. 11110011 = ________ change the sign by cutting off ALL the sign bits.
11.21 11.22 Data Representation • In C/C++ variables can be of different types and sizes – Integer Types (signed and unsigned) C Type Bytes Bits ATmega328 [unsigned] char 1 8 byte [unsigned] short [int] 2 16 word - 1 [unsigned] long [int] 4 32 [unsigned] long long [int] 8 64 - 1 int ? 2 ? 2 ? 2 ARITHMETIC 1 Can emulate but has no single-instruction support 2 Varies by compiler/machine (avr-gcc: int = 2 bytes, g++ for x86: int = 4-bytes) – Floating Point Types C Type Bytes Bits ATmega328 float 4 32 N/A 1 double 8 64 N/A 1 11.23 11.24 Binary Arithmetic Binary Addition • Can perform all arithmetic operations (+,-,*, ÷ ) on binary • In decimal addition we ______ when the sum is 10 or more numbers • In binary addition we carry when the sum is __ or more • Can use same methods as in decimal • Add bits in binary to produce a sum bit and a carry bit – Still use carries and borrows, etc. – Only now we carry when sum is 2 or more rather than 10 or 1 more (decimal) 0 1 1 0 – We borrow 2’s not 10’s from other columns + 1 + 0 + 1 + 0 • Easiest method is to add bits in your head in decimal 01 01 10 00 (1+1 = 2) then convert the answer to binary (2 10 = 10 2 ) no need sum bit no need sum bit carry 1 sum bit no need sum bit to carry to carry into next to carry column of bits
11.25 11.26 Binary Addition & Subtraction Binary Addition 1 2 0 10 0 0 0110 (6) 0110 (6) 1 + 1 + 0111 (7) + 0111 (7) + 1 01 1101 (13) 1101 (13) 10 0 1 1 1 1 0 1 0 + 0 0 1 1 - 0 1 0 1 carry bit sum bit carry bit sum bit 3 110 1 4 110 1 0110 (6) 0 0110 (6) 1 + 0111 (7) + 0 + 0111 (7) + 1 1101 (13) 01 1101 (13) 11 carry bit sum bit carry bit sum bit 11.27 11.28 Hexadecimal Arithmetic • Same style of operations – Carry when sum is 16 or more, etc. 4 D 16 "Taking the 2's complement" 16 1 SUBTRACTION THE EASY WAY + B 5 16 16 1
Recommend
More recommend