Two’s Complement & Binary Arithmetic

Slides:



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

James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer?
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
Signed Numbers.
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Complements: different approaches for representing negative numbers
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.
Two’s Complement 1.As an action: (Assume the starting value is 1011) 1.Flip the bits from the starting value => Add one to get the answer.
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.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
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.
1 Positive numbers are well understood An n-bit number represents numbers from 0 to 2 n -1 n+m bits can be used to represent n-bit integer and m-bit fraction.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Addition and Substraction
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Monday, January 13 CEC 220 Digital Circuit Design Slide 1 of 14.
CEC 220 Digital Circuit Design Binary Arithmetic & Negative Numbers Fri, Aug 28 CEC 220 Digital Circuit Design Slide 1 of 14.
Integers’ Representation. Binary Addition. Two's Complement. Unsigned number representation Binary Addition, Subtraction. Overflow of unsigned numbers.
James Tam Numerical Representations On The Computer: Negative And Rational Numbers How are negative and rational numbers represented on the computer? How.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Computer Organization 1 Data Representation Negative Integers.
ECE 3110: Introduction to Digital Systems Signed Number Conversions and operations.
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:
Advanced Binary b. describe and use two’s complement and sign and magnitude to represent negative integers; c. perform integer binary arithmetic, that.
ECE 2110: Introduction to Digital Systems Signed Number Conversions.
Lecture 4: Digital Systems & Binary Numbers (4)
Negative Number Sign-Magnitude: left-most bit as the sign bit –16 bits –Example: 4-bit numbers is given by is given by ’s complement:
Cosc 2150: Computer Organization
Design of Digital Circuits Reading: Binary Numbers
Lec 3: Data Representation
Data Representation.
Negative Numbers and Subtraction
Negative numbers: Week 10 Lesson 1
Chapter 4 Operations on Bits.
CHAPTER 9 COMPUTER ARITHMETIC - ALU
Integers’ Representation. Binary Addition. Two's Complement.
3.1 Denary, Binary and Hexadecimal Number Systems
Digital Logic & Design Dr. Waseem Ikram Lecture 02.
Binary Arithmetic Binary arithmetic is essential in all digital computers and in many other types of digital systems. Addition, Subtraction, Multiplication,
Data Representation in Computer Systems
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
IT 0213: INTRODUCTION TO COMPUTER ARCHITECTURE
Addition and Substraction
Binary Addition & Subtraction
Teaching Computing to GCSE
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Fundamentals of Data Representation
Digital Logic & Design Lecture 02.
Teaching Computing to GCSE
Unit 18: Computational Thinking
C1 Number systems.
How are negative and rational numbers represented on the computer?
CPS120: Introduction to Computer Science
Number Systems Rayat Shikshan Sanstha’s
Number Systems Rayat Shikshan Sanstha’s
Binary to Decimal Conversion
ECE 331 – Digital System Design
23/04/2019 Data Representation Conversion.
Data Binary Arithmetic.
GCSE COMPUTER SCIENCE Topic 3 - Data 3.2 Signed Integers.
COMS 361 Computer Organization
Chapter3 Fixed Point Representation
Chapter 1 (Part c) Digital Systems and Binary Numbers
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Theory: 2.6 – Data Representation
Today Binary addition Representing negative numbers 2.
Presentation transcript:

Two’s Complement & Binary Arithmetic 4.12 Data Representation Two’s Complement & Binary Arithmetic 29/09/2019

Learning Objectives: Describe and use two's complement and sign and magnitude to represent positive and negative integers. Perform integer binary arithmetic: addition and subtraction. 29/09/2019

Representing Negative Numbers - Sign & Magnitude As there is no third symbol available to store a negative symbol explicitly we must use a bit to show if a number is negative or not. We name this bit the ‘Sign Bit’ We use the leftmost bit. If the ‘Sign Bit’ is 1 then the number is negative, if it is 0 then it is positive. 29/09/2019

Representing Negative Numbers - Sign & Magnitude At first glance it may appear to be simple from this point, for example: 3 = 0 0000011 so -3 = 1 0000011 Sign Bits 29/09/2019

Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with sign and magnitude binary numbers. 29/09/2019 5

Binary Arithmetic Rules 0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 0 (carry 1) 1+1+1 = 1 (carry 1) 29/09/2019

Problems with Sign & Magnitude Addition and subtraction calculations give incorrect results: e.g. +3 + -3 = 0 but 0 0000011 0 00000011 +1 0000011 -1 00000011 1 0000110 i.e. not 0 0 00000000 This is due to the fact that the sign bit does not have a place value. Rules to correct this error: If the signs are the same, add the magnitudes as unsigned numbers and, if there is a “carry out”, use it as the MSB (last bit). If the signs differ, subtract the smaller magnitude from the larger, and keep the sign of the larger e.g. http://www.cs.uwm.edu/classes/cs315/Bacon/Lecture/HTML/ch04s11.html To correct this error: 29/09/2019

Problems with Sign & Magnitude Also note that this method allows two representations of 0: 1 0000000 0 0000000 This will cause problems for a processor. 29/09/2019 8

Two’s Complement This is a better way to represent negative numbers. Imagine a km clock in a car set at 00000000 kilometres. If the car goes forward one km the reading becomes 00000001. If the meter was turned back one km the reading would be 99999999 km. This could be interpreted as ‘-1’ km. 29/09/2019

Two’s Complement Sign Bit So: 0 0000011 = 3 0 0000010 = 2 0 0000001 = 1 0 0000000 = 0 1 1111111 = -1 1 1111110 = -2 1 1111101 = -3 Sign Bit 29/09/2019

Binary – Decimal Spreadsheet Converter 2 Try using it to ‘play’ with two’s complement binary numbers. 29/09/2019

Using Two’s Complement Negative denary to binary Work out the binary number as if it were positive. From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. Flip means change 0 to 1 or 1 to 0. Negative binary to denary Reverse of above 29/09/2019

-5 -5 = 11111011 Work out the binary number as if it were positive. 5 = 0 0000101 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. -5 = 11111011 Don’t flip the last 1. 1 1 1 1 1 1 1 29/09/2019

11111011 From the left, flip all bits up to the last ‘1’, leave this and any other bits after that alone. 00000101 Work out the decimal number as if it were positive. 0 0000101 = 5 Add the minus sign. 11111011 = -5 29/09/2019

Alternative way of using Two’s Complement The MSB stays as a number, but is made negative. This means that the column headings are -128 64 32 16 8 4 2 1 +117 does not need to use the MSB, so it stays as 01110101. -117 = -128 + 11 = -128 + (8 + 2 + 1) Fitting this in the columns gives 10001011 29/09/2019

Two’s Complement Now addition and subtraction calculations give the correct results: 0 0000011 = 3 + 1 1111101 = -3 10 0000000 = 0 11 111111 <- carries Notes: The last ‘carry’ of 1 (carry in and out of the Most Significant Bit – MSB) has to be ignored unless an overflow has occurred (see next slide). The arithmetic works here, as all the bits, including the sign bit, in this method have a place value. There is only one representation for zero. 00000000 = 0 10000000 = -128 (not 0 as in sign & magnitude) 29/09/2019

Problem with Two’s Complement +102 = 01100110 +117 = 01110101 102+117 = 219 but 01100110 + 01110101 11011011 = -37 11 1 <- carries The original numbers are positive but the answer is negative! There has been an overflow from the positive part of the byte to the negative. To solve this error: If an "overflow" occurs add an extra bit and use this as the new sign bit. An overflow in a two's complement sum has occurred if: The sum of two positive numbers gives a negative result. The sum of two negative numbers gives a positive result. e.g. For the example above: 011011011 = 219 (which is correct). New Sign bit (-256) old sign bit = +128

Plenary A particular computer stores numbers as 8 bit, two’s complement, binary numbers. 01011101 and 11010010 are two numbers stored in the computer. Write down the decimal equivalent of 11010010. Add the two binary values together and comment on your answer. 29/09/2019

Plenary -46 00101111 = +47 A positive and negative have been added together and the result is positive. Because the larger value was positive. There was carry in and out of MSB therefore ignore carry out, (result is correct). 29/09/2019