ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.

Slides:



Advertisements
Similar presentations
Lecture no 6. Two's Complement Given a negative number (N), represented using the Two's Complement representation (N*), the magnitude of the number (P)
Advertisements

HEXADECIMAL NUMBERS Code
Addition and Subtraction. Outline Arithmetic Operations (Section 1.2) – Addition – Subtraction – Multiplication Complements (Section 1.5) – 1’s complement.
Chapter 2: Data Representation
Principles of Computer Architecture Miles Murdocca and Vincent Heuring Chapter 2: Data Representation.
COE 202: Digital Logic Design Signed Numbers
CHAPTER 2 Number Systems, Operations, and Codes
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Number Systems Standard positional representation of numbers:
Signed Numbers.
Number Systems Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
VIT UNIVERSITY1 ECE 103 DIGITAL LOGIC DESIGN CHAPTER I NUMBER SYSTEMS AND CODES Reference: M. Morris Mano & Michael D. Ciletti, "Digital Design", Fourth.
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Signed Numbers CS208. Signed Numbers Until now we've been concentrating on unsigned numbers. In real life we also need to be able represent signed numbers.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
1.6 Signed Binary Numbers.
Binary Representation - Shortcuts n Negation x + x = 1111…1111 two = -1 (in 2’s complement) Therefore, -x = x + 1 n Sign Extension o Positive numbers :
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
Simple Data Type Representation and conversion of numbers
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Data Representation – Binary Numbers
Computer Arithmetic Nizamettin AYDIN
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
ECE 3110: Introduction to Digital Systems Review #1 (Chapter 1,2)
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles of Computer.
Data Representation and Computer Arithmetic
Complement Numbers. Outline  Negative Numbers Representation  Sign-and-magnitude  1s Complement  2s Complement  Comparison of Sign-and-Magnitude.
Number Systems. Why binary numbers? Digital systems process information in binary form. That is using 0s and 1s (LOW and HIGH, 0v and 5v). Digital designer.
1 Digital Design: Number Systems Credits : Slides adapted from: J.F. Wakerly, Digital Design, 4/e, Prentice Hall, 2006 C.H. Roth, Fundamentals of Logic.
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
BR 8/99 Binary Numbers Again Recall than N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to 255.
ECE 3110: Introduction to Digital Systems unsigned Multiplication/Division codes.
ECE2030 Introduction to Computer Engineering Lecture 2: Number System Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech.
Number Systems Decimal (Base 10) –10 digits (0,1,2,3,4,5,6,7,8,9) Binary (Base 2) –2 digits (0,1) Digits are often called bits (binary digits) Hexadecimal.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Operations on Bits Arithmetic Operations Logic Operations
Number Systems Binary to Decimal Octal to Decimal Hexadecimal to Decimal Binary to Octal Binary to Hexadecimal Two’s Complement.
ECE 331 – Digital System Design Representation and Binary Arithmetic of Negative Numbers and Binary Codes (Lecture #10) The slides included herein were.
INEL 4215: Computer Architecture and Organization Number Systems Signed numbers.
AEEE2031 Data Representation and Numbering Systems.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Addition and Substraction
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Digital Design Basics Analog vs Digital Why we need digital? Reproducibility, economy, programmability… Digital Devices Gates, FFs Combinational, sequential.
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
IT1004: Data Representation and Organization Negative number representation.
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic Herbert G. Mayer, PSU Status 1/14/2016 Copied with Permission from prof. Mark PSU ECE.
ECE 2110: Introduction to Digital Systems
In decimal we are quite familiar with placing a “-” sign in front of a number to denote that it is negative The same is true for binary numbers a computer.
ECE 3110: Introduction to Digital Systems Number Systems: signed numbers.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Nguyen Le CS147.  2.4 Signed Integer Representation  – Signed Magnitude  – Complement Systems  – Unsigned Versus Signed Numbers.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Signed binary numbers & Binary Codes
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Addition and Substraction
ECE 2110: Introduction to Digital Systems
ECE 3110: Introduction to Digital Systems
CPS120: Introduction to Computer Science
ECE 331 – Digital System Design
Two’s Complement & Binary Arithmetic
Presentation transcript:

ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations

2 Previous class Summary Signed-magnitude, one’s complement Different for negatives numbers Representations of positive numbers are SAME. 0 may have different representations. Sign bit: 0 for positive, 1 for negative

3 Radix-Complement Given an n-digit number D; Complement is obtained by subtracting D from r n <----complementing each individual digit and adding 1. Decimal (r=10): 10’s complement Binary (r=2): two’s complement

4 Twos Complement Representation To encode a negative number, get the binary representation of its magnitude, COMPLEMENT each bit, then ADD 1. (get Ones complement, then add 1). What is -5 in Twos Complement, 8 bits? The magnitude 5 in 8-bits is = Now complement each bit: = FA 16 Now add one: FA = FB 16 FB 16 is the 8-bit, twos complement representation of -5. NOTE: positive numbers in 2s complement are simply their binary representation.

5 Twos Complement Examples -5 = = FB = = = = 7F = = = = (note the extended range!) + 0 = = = = (only 1 zero!!!) For 8 bits, can represent the signed integers -128 to For N bits, can represent the signed integers -2 (N-1) to + 2 (N-1) - 1 Note that negative range extends one more than positive range.

6 Twos Complement Comments Twos complement is the method of choice for representing signed integers. It has none of the drawbacks of Signed Magnitude or Ones Complement. There is only one zero, and K + (-K) = = FB = = 0 !!! Normal binary addition is used for adding numbers that represent twos complement integers.

7 A common Question? Given a hex number, how do I know if it is in 2’s complement or 1’s complement; is it already in 2’s complement or do I have put it in 2’s complement, etc, yadda,yadda, yadda….

8 Answer If I write a HEX number, I will ask for a decimal representation if you INTERPRET the encoding as a particular method (i.e, either 2’s complement, 1’s complement, signed magnitude). A Hex or binary number BY ITSELF can represent ANYTHING (unsigned number, signed number, character code, colored llamas, etc). You MUST HAVE additional information that tells you what the encoding of the bits mean.

9 Excess representation Excess-B representation Bias Excess-2 m-1 system

10 Hex to Signed Decimal Conversion Rules Given a Hex number, and you are told to convert to a signed integer (either as signed magnitude, 1s complement, 2s complement) STEP 1: Determine the sign! If the Most Significant Bit is zero, the sign is positive. If the MSB is one, the sign is negative. This is true for ALL THREE representations: SM, 1s complement, 2s complement. F0 16 = (MSB is ‘1’), so sign of result is ‘-’ = (MSB is ‘0’), so sign of result is ‘+’. If the Most Significant Hex Digit is > 7, then MSB = ‘1’ !!! (eg, 8,9,A,B,C,D,E,F => MSB = ‘1’ !!!)

11 Hex to Signed Decimal (cont) STEP 2 (positive sign): If the sign is POSITIVE, then just convert the hex value to decimal. The representation is the same for SM, 1s complement, 2s complement is a positive number, decimal value is 6 x = 100. Final answer is +100 regardless of whether encoding was SM, 1s complement, or 2s complement as an 8 bit signed magnitude integer = as an 8 bit ones complement integer = as an 8 bit twos complement integer = +100

12 Hex to Signed Decimal (cont) STEP 2 (negative sign): If the sign is Negative, then need to compute the magnitude of the number. We will use the trick that - (-N) = + N i.e. Take the negative of a negative number will give you the positive number. In this case the number will be the magnitude. If the number is SM format, set Sign bit to Zero: F0 16 = => = = 112 If the number is 1s complement, complement each bit. F0 16 = => = 0F 16 = 15 If the number is 2s complement, complement and add one. F0 16 = => = = = 16

13 Hex to Signed Decimal (cont) STEP 3 : Just combine the sign and magnitude to get the result. F0 16 as 8 bit Signed magnitude number is -112 F0 16 as 8 bit ones complement number is -15 F0 16 as 8 bit twos complement number is as an 8 bit signed magnitude integer = as an 8 bit ones complement integer = as an 8 bit twos complement integer = +100

14 Example Conversions FE 16 as an 8 bit unsigned integer = 254 FE 16 as an 8 bit signed magnitude integer = -126 FE 16 as an 8 bit ones complement integer = - 1 FE 16 as an 8 bit twos complement integer = -2 7F 16 as an 8 bit unsigned integer = 127 7F 16 as an 8 bit signed magnitude integer = F 16 as an 8 bit ones complement integer = F 16 as an 8 bit twos complement integer = +127 To do hex to signed decimal conversion, we need to determine sign (Step 1), determine Magnitude (step 2), combine sign and magnitude (Step 3)

15 Signed Decimal to Hex conversion Step 1: Know what format you are converting to!!! You must know if you are converting the signed decimal to SM, 1s complement, or 2s complement. Convert +34 to all three formats. Convert -20 to all three formats Step 2: Ignore the sign, convert the magnitude of the number to binary. 34 = 2 x = = = 1 x = =

16 Signed Decimal to Hex conversion (cont) Step 3 (positive decimal number): If the decimal number was positive, then you are finished no matter what the format is! +34 as an 8 bit SM number is = as an 8 bit 1s complement number is = as an 8 bit 2s complement number is =

17 Signed Decimal to Hex conversion (cont) Step 3 (negative decimal number): Need to do more if decimal number was negative. To get the final representation, we will use the trick that: - (+N) = -N i.e., if you take the negative of a positive number, get Negative number. If converting to SM format, set Sign bit to One: 20 = => = If converting to 1s complement, complement each bit. 20 = => = EB 16 If converting to 2s complement, complement and add one. 20 = => = % = EC 16

18 Signed Decimal to Hex conversion (cont) Final results: +34 as an 8 bit SM number is = as an 8 bit 1s complement number is = as an 8 bit 2s complement number is = as an 8 bit SM number is = as an 8 bit 1s complement number is EB 16 = as an 8 bit 2s complement number is EC 16 =

19 signed addition/subtraction Two’s-complement Addition rules: same as ordinary binary addition, ignoring any carries beyond the MSB. Subtraction rules: Negate the subtrahend by taking its 2’s complement, then add it to the minuend as above. Overflow If an addition/subtraction operation produces a result that exceeds the range of the number system, overflow is said to occur. Rules for detecting overflow in unsigned and signed numbers are different.

20 Two’s Complement Overflow Consider two 8-bit 2’s complement numbers. I can represent the signed integers -128 to +127 using this representation. What if I do (+1) + (+127) = The number +128 is OUT of the RANGE that I can represent with 8 bits. What happens when I do the binary addition? +127 = 7F = /= (this is actually -128 as a twos complement number!!! - the wrong answer!!!) How do I know if overflowed occurred? Added two POSITIVE numbers, and got a NEGATIVE result.

21 Detecting Two’s Complement Overflow Two’s complement overflow occurs is: Add two POSITIVE numbers and get a NEGATIVE result Add two NEGATIVE numbers and get a POSITIVE result We CANNOT get two’s complement overflow if I add a NEGATIVE and a POSITIVE number together. The Carry out of the Most Significant Bit means nothing if the numbers are two’s complement numbers. While it means overflow in unsigned numbers.

22 Some Examples All hex numbers represent signed decimal in two’s complement format. FF 16 = = = 0 Note there is a carry out, but the answer is correct. Can’t have 2’s complement overflow when adding positive and negative number. FF 16 = = F 16 = +127 (incorrect!!) Added two negative numbers, got a positive number. Twos Complement overflow.

23 Adding Precision (unsigned) What if we want to take an unsigned number and add more bits to it? Just add zeros to the left. 128 = (8 bits) = (16 bits) = (32 bits)

24 Adding Precision (two’s complement ) What if we want to take a twos complement number and add more bits to it? Take whatever the SIGN BIT is, and extend it to the left = = (8 bits) = FF80 16 = (16 bits) = FFFFFF80 16 (32 bits) = 7F 16 = (8 bits) = 007F 16 = ( 16 bits) = F 16 (32 bits) This is called SIGN EXTENSION. Extending the MSB to the left works for two’s complement numbers and unsigned numbers.

25 Signed 2’s-complement and unsigned binary numbers Basic binary addition and subtraction algorithms are same Interpretation may be different

26 What do you need to know? Addition, subtraction of binary, hex numbers Detecting unsigned overflow Number ranges for unsigned, SM, 1s complement, 2s complement Overflow in 2s complement Sign extension in 2s complement

27 Next… Binary Multiplication/divison BCD, GRAY, and other codes