Representing Positive and Negative Numbers

Slides:



Advertisements
Similar presentations
Addition and Subtraction. Outline Arithmetic Operations (Section 1.2) – Addition – Subtraction – Multiplication Complements (Section 1.5) – 1’s complement.
Advertisements

EMB1006 The Binary System There is no 2 Jonathan-Lee Jones.
CS 151 Digital Systems Design Lecture 3 More Number Systems.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
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.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Lecture 12: Computer Arithmetic Today’s topic –Numerical representations –Addition / Subtraction –Multiplication / Division 1.
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
Introduction to Computer Engineering ECE/CS 252, Fall 2010 Prof. Mikko Lipasti Department of Electrical and Computer Engineering University of Wisconsin.
CPS120: Introduction to Computer Science Computer Math: Signed Numbers.
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.
Lecture 2 Binary Values and Number Systems. The number 943 is an example of a number written in positional notation. The relative positions of the digits.
Operations on Bits Arithmetic Operations Logic Operations
THE BINARY SYSTEM.
AEEE2031 Data Representation and Numbering Systems.
By Jariya Phongsai A two's-complement system is a system in which negative numbers are represented by the two's complement of the absolute value; this.
Sep 29, 2004Subtraction (lvk)1 Negative Numbers and Subtraction The adders we designed can add only non-negative numbers – If we can represent negative.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
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.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
Integer Operations Computer Organization and Assembly Language: Module 5.
Computer Organization 1 Data Representation Negative Integers.
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.
1 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
Binary Addition The simplest arithmetic operation in binary is addition. Adding two single-digit binary numbers is relatively simple, using a form of carrying:
Lecture 4: Digital Systems & Binary Numbers (4)
Answer CHAPTER FOUR.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Floating Point Numbers Dr. Mohsen NASRI College of Computer and Information Sciences, Majmaah University, Al Majmaah
William Stallings Computer Organization and Architecture 8th Edition
Floating Point Representations
Department of Computer Science Georgia State University
Cosc 2150: Computer Organization
Addition and Subtraction
Negative Numbers and Subtraction
Negative Integers Unsigned binary representation can not be used to represent negative numbers. With paper and pencil arithmetic, a number is made negative.
Chapter 4 Operations on Bits.
Negative Binary Numbers
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Integer Real Numbers Character Boolean Memory Address CPU Data Types
Computer Science 210 Computer Organization
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
CHAPTER 4: Representing Integer Data
Addition and Substraction
ECE 2110: Introduction to Digital Systems
Wakerly Section 2.4 and further
Data Structures Mohammed Thajeel To the second year students
Computer Science 210 Computer Organization
Lecture 8: Addition, Multiplication & Division
Number Representation
Data Representation in Computer Systems
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Digital Logic & Design Lecture 02.
ECE 3110: Introduction to Digital Systems
Unit 18: Computational Thinking
COMS 161 Introduction to Computing
CPS120: Introduction to Computer Science
Binary to Decimal Conversion
ECE 171 Digital Circuits Chapter 2 Binary Arithmetic
CSC 220: Computer Organization Signed Number Representation
COMS 361 Computer Organization
Chapter3 Fixed Point Representation
Today Binary addition Representing negative numbers 2.
Presentation transcript:

Representing Positive and Negative Numbers

Negative numbers The sign (+/-) can be represented using an additional bit known as the sign bit. There are several different methods for encoding the sign. The simplest method is known as sign-magnitude representation.

Sign-magnitude representation Let the high-order bit serve as the sign bit. A positive value has a sign bit of 0. A negative value has a sign bit of 1. Can represent values from -(2(n-1)-1) to +(2(n-1)-1) Exercise: signed-magnitude values for the integers -7 to +7…

Sign-magnitude representation: 4 bits Decimal Signed magnitude +7 0111b +6 0110b +5 0101b +4 0100b +3 0011b +2 0010b +1 0001b +0 0000b -0 1000b -1 1001b -2 1010b -3 1011b -4 1100b -5 1101b -6 1110b -7 1111b There are two possible representations for zero. Negative values have sign bit set to 1

Exercise 2.11 What is the 8-bit signed-magnitude binary representation for each of the following decimal numbers? (a) 23 (b) -23 (c) -48 See exercise 2.11 on page 39 of Computer Architecture by N. Carter

Exercise: Solution 2.11 What is the 8-bit signed-magnitude binary representation for each of the following decimal numbers? (a) 23 00010111b (b) -23 10010111b (c) -48 10110000b The sign bit is highlighted in red See exercise 2.11 on page 39 of Computer Architecture by N. Carter

Sign-magnitude representation Can negate a number simply by inverting the sign bit. Test if a value is positive or negative by checking the sign bit. Easy to perform multiplication or division Just perform unsigned multiplication or division Set sign bit of the result based on sign bits of operands positive x positive = positive positive x negative = negative negative x negative = positive Addition and subtraction present a very difficult problem.

Sign-magnitude representation Multiply the numbers +7 and -5 using 6-bit signed-magnitude representation. +7 = 000111b -5 = 100101b 7x5 = 0100011b positive x negative = negative so set the sign bit Solution: Answer = 1100011b

Sign-magnitude representation Try to directly add 8-bit signed-magnitude values for +10 and -4. Solution: +10 = 00001010b -4 = 10000100b 00001010b + 10000100b 10001110b The sum is 10001110b, which is interpreted as -14 (Wrong!)

Sign-magnitude representation A better solution: Try a different representation for signed binary numbers?

Proposed Solution: Invert the bits 01010 = +9 10101 = -9 Leftmost bit is 1 indicating negative Does addition of 9 + -9 give zero? No? Let’s try something different…

Two’s Complement Representation To obtain a two’s complement representation of a negative number... Find the unsigned binary integer representation (2) Invert each bit (3) Add 1 to the result (4) Discard any overflow bits Two’s complement for a positive number is the same as its unsigned binary representation

Example: Two’s Complement Representation What is the 6-bit two’s complement representation of -12? Solution: (1) unsigned 12 001100 (2) invert bits 110011 (3) add 1 110100 -12 = 110100 in 6-bit two’s complement As with signed magnitude, a 1 in the leftmost bit means negative!

Two’s Complement Representation What is the result of adding +12 and -12 in 6-bit two’s complement? Solution: +12 001100 -12 +110100 ----------------------------------------- 1 000000 Discard the 7th (leftmost) overflow bit So our answer is 0. 12 + -12 = 0. Now that’s much better.

Range of Represented Values Four-bit two’s complement: Write 0-7, Fill-in -1, -2, … -8 0 = 0000 -8 = 1000 1 = 0001 -7 = 1001 2 = 0010 -6 = 1010 3 = 0011 -5 = 1011 4 = 0100 -4 = 1100 5 = 0101 -3 = 1101 6 = 0110 -2 = 1110 7 = 0111 -1 = 1111 There is only one representation for zero Range of values is -8 to +7 inclusive -2^(n-1) to +(2^(n-1))-1

What are Max_int and Min_int in 16 bits?

What are Max_int and Min_int in 16 bits?

Exercises Answer: Same as unsigned representation (a) 23 (b) -23 2.12 What is the 8-bit two’s complement binary representation for the following decimal numbers? (a) 23 Answer: Same as unsigned representation (b) -23 Answer: (c ) 57 (d) -57 See exercise 2.12 on page 39 of Computer Architecture by N. Carter

Exercises (a) 23 Answer: 00010111b Same as unsigned representation 2.12 What is the 8-bit two’s complement binary representation for the following decimal numbers? (a) 23 Answer: 00010111b Same as unsigned representation (b) -23 Answer: 11101001b (c ) 57 Answer: 00111001b (d) -57 Answer: 11000111b See exercise 2.12 on page 39 of Computer Architecture by N. Carter

Negation in Two’s Complement Given a binary number in two’s complement, form its negative by inverting its bits and adding one. This works regardless of whether the original two’s complement binary number is positive or negative (leftmost bit is 1). Negate the 4-bit two’s complement representation of +5 twice. Begin with the two’s complement representation of +5 Negate it, this is the representation of -5 Negate it again, you should be back to +5

From Two’s Complement to Base Ten (a) What is the base ten equivalent for this 6-bit two’s complement value? 011001 Answer: 25 Sign bit is 0, read its value the usual way (b) What is the decimal equivalent of this 6-bit two’s complement value? 100011 Answer: Sign bit is 1 so it’s a negative value. How do we find its absolute value (magnitude)? 011100 Negate it by inverting bits and adding 1 + 000001 011101 Magnitude is 29 but negative sign means -> -29

Exercises Convert these 5-bit two’s complement values into decimal. Answer: (b) 10001 (c) 11111 (d ) 10000

Exercises Convert these 5-bit two’s complement values into decimal. Answer: -5 (b) 10001 Answer: -15 (c) 11111 Answer: -1 (d ) 10000 Answer: -16

Addition in Two’s Complement Addition is correctly computed by directly adding the bits. Compute X-Y by computing X + (-Y) using negation of Y.

Exercises Add the values +3 and -4 in two’s complement notation using 4 bits. Compute -3 - 4 in two’s complement notation using 5-bit numbers.

Exercises: Solution Negate the 4-bit two’s complement representation of +5 twice. 0101 (+5)  1010 + 0001 = 1011 (-5)  0100 + 0001 = 0101 (+5) Negating twice results in the original number as we would expect Add the values +3 and -4 in two’s complement notation. 0011 (+3) 0100 (+4)  1011 + 0001 = 1100 (-4) +1100 (-4) 1111 (-1) Compute -3 - 4 in two’s complement notation. 11101 (-3) +11100 (-4) 11001 (-7) 11001  00110 + 00001 = 00111 (+7)  Means to negate the two’s complement value

Useful Properties of Two’s Complement Sign is determined by examining the high-order bit. Negating a number twice results in the original number. Addition is correctly computed by directly adding the bits. Compute X-Y by computing X+(-Y) using negation of Y. Represents values in range -(2(n-1)) to +(2(n-1)-1) Only one representation for zero. Due to these properties, two’s complement notation is used in all modern computers.

Properties of Two’s Complement Multiplication in two’s complement is more complex than with signed-magnitude notation. Addition and subtraction in two’s complement is easier than with signed-magnitude notation. Why would two’s complement still be favored? Answer: Addition and subtraction operations tend to be more frequent than multiplication. Designers choose the fastest way to perform the more frequent computation.

Sign Extension Given two binary numbers of differing numbers of bits. Example: Four bit number and Eight bit number 1001b and 01101100b Sometimes necessary to convert the shorter number to the same number of bits as the larger number before doing arithmetic If the two numbers are unsigned, then simply append extra 0 bits to the high-order end of the shorter number Example: Append an extra four 0 bits to extend to 8 bits 0000  1001b = 00001001b

Sign Extension for Sign-Magnitude Given a binary number expressed in sign-magnitude format Extend the number by appending the extra bits set to 0 Copy the given sign bit into the high-order bit position Set the former sign bit to 0 Example: Sign extend the 4-bit sign-magnitude number 1001b to 8 bits. This is -1 (base 10) in sign magnitude. 1001b 1000 1001b 10000001b Copy original sign bit into new high-order bit Clear former sign bit 10000001b (-1 in sign magnitude)

Sign Extension for Sign-Magnitude Problem: What is the 16-bit sign-magnitude representation of the 8-bit sign-magnitude 10000111 (-7)? Solution: 00000000 10000111 // Preppend extra 0 bits 10000000 00000111 // Move original sign bit into new // high-order bit position 1000000000000111

Sign Extension for Two’s Complement Given 2’s complement binary number Extend the number by appending the extra bits Set all of the extra bits to the original sign bit value Example: Sign-extend the 4-bit two’s complement number 1001b to 8 bits. 1001b 11111001b

Sign Extension for Two’s Complement Problem: What is the 16-bit sign extension of the 8-bit two’s complement value 10010010 (-110)? Solution: 00000000 10010010 // Preppend 8 extra bits 11111111 10010010 // Set extra bits to original sign // bit value 11111111 10010010