Binary numbers and arithmetic. ADDITION Addition (decimal)

Slides:



Advertisements
Similar presentations
Integers. Integer Storage Since Binary consists only of 0s and 1s, we can’t use a negative sign ( - ) for integers. Instead, the Most Significant Bit.
Advertisements

COE 202: Digital Logic Design Signed Numbers
CS 151 Digital Systems Design Lecture 3 More Number Systems.
1 Lecture 3 Bit Operations Floating Point – 32 bits or 64 bits 1.
Signed Numbers.
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Floating Point Numbers
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
S. Barua – CPSC 240 CHAPTER 2 BITS, DATA TYPES, & OPERATIONS Topics to be covered are Number systems.
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.
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
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.
Binary Number Systems.
School of Computer Science G51CSA 1 Computer Arithmetic.
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 :
The Binary Number System
Data Representation Number Systems.
Computer Organization & Programming Chapter2 Number Representation and Logic Operations.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Lecture Overview Introduction Positional Numbering System
46 Number Systems Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
Number Systems and Number Representation Jennifer Rexford 1.
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.
ECE2030 Introduction to Computer Engineering Lecture 2: Number System Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Tech.
ECE 2110: Introduction to Digital Systems Signed Addition/Subtraction.
Operations on Bits Arithmetic Operations Logic Operations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Floating Point Arithmetic
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.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
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=
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
2’s Complement Another system that lets us represent negative numbers
IT1004: Data Representation and Organization Negative number representation.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
ECEN 248: INTRODUCTION TO DIGITAL SYSTEMS DESIGN Lecture 8 Dr. Shi Dept. of Electrical and Computer Engineering.
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:
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Numbers Dr. Mohsen NASRI College of Computer and Information Sciences, Majmaah University, Al Majmaah
Floating Point Arithmetic – Part I
CHAPTER 5: Representing Numerical Data
Cosc 2150: Computer Organization
Numbers How does computer understand numbers? Knows only two symbols
David Kauchak CS 52 – Spring 2017
Addition and Subtraction
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Binary numbers and arithmetic
CHAPTER 4: Representing Integer Data
CS1010 Programming Methodology
ECE 2110: Introduction to Digital Systems
Data Structures Mohammed Thajeel To the second year students
Lecture 2 Topics Binary Arithmetic (Unsigned binary operands)
Data Representation in Computer Systems
Unit 18: Computational Thinking
How are negative and rational numbers represented on the computer?
CSC 220: Computer Organization Signed Number Representation
COMS 361 Computer Organization
Chapter 1 Introduction.
Two’s Complement & Binary Arithmetic
Presentation transcript:

Binary numbers and arithmetic

ADDITION

Addition (decimal)

Addition (binary)

So can we count in binary?

Counting in binary (4 bits) …

MULTIPLICATION

Multiplication (decimal)

Multiplication (binary)

It’s interesting to note that binary multiplication is a sequence of shifts and adds of the first term (depending on the bits in the second term is missing here because the corresponding bit in the second terms is 0.

REPRESENTING SIGNED (POSITIVE AND NEGATIVE) NUMBERS

Representing numbers (ints) Fixed, finite number of bits. bitsbytesC/C++IntelSun 81char[s]bytebyte 162short[s]wordhalf 324int or long[s]dwordword 648long long[s]qwordxword

Representing numbers (ints) Fixed, finite number of bits. bitsIntelsignedunsigned 8[s]byte [s]word [s]dword [s]qword In general, for k bits, the unsigned range is [0..+2 k -1] and the signed range is [-2 k k-1 -1].

Methods for representing signed ints. 1.signed magnitude 2.1’s complement (diminished radix complement) 3.2’s complement (radix complement) 4.excess b D-1

Signed magnitude Ex. 4-bit signed magnitude – 1 bit for sign – 3 bits for magnitude

Signed magnitude Ex. 4-bit signed magnitude – 1 bit for sign – 3 bits for magnitude

1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D -1+(-x) where b = base D = (total) # of bits (including the sign bit) Ex. Let b=2 and D=4. Then -1 is represented by = or

1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D -1+(-x) where b = base & D = (total) # of bits (including the sign bit) Ex. What is the 9’s complement of ? Given b=10 and D=5. Then the 9’s complement of = 10 5 – 1 – = – 1 – = – = 87610

1’s complement (diminished radix complement) Let x be a non-negative number. Then –x is represented by b D - 1+(-x) where b = base D = (total) # of bits (including the sign bit) Shortcut for base 2? – All combinations used, but 2 zeros!

2’s complement (radix complement) Let x be a non-negative number. Then –x is represented by b D +(-x). – Ex. Let b=2 and D=4. Then -1 is represented by = 15 or – Ex. Let b=2 and D=4. Then -5 is represented by 2 4 – 5 = 11 or – Ex. Let b=10 and D=5. Then the 10’s complement of = 10 5 – = – =

2’s complement (radix complement) Let x be a non-negative number. Then –x is represented by b D +(- x). – Ex. Let b=2 and D=4. Then -1 is represented by = 15 or – Ex. Let b=2 and D=4. Then -5 is represented by 2 4 – 5 = 11 or Shortcut for base 2?

2’s complement (radix complement) Shortcut for base 2? – Yes! Flip the bits and add 1.

2’s complement (radix complement) Are all combinations of 4 bits used? – No. (Now we only have one zero.) – 1000 is missing! What is 1000? Is it positive or negative? Does = -7 work in 2’s complement?

excess b D-1 (biased representation) For pos, neg, and 0, x is represented by b D-1 + x Ex. Let b=2 and D=4. Then the excess 8 (2 4-1 ) representation for 0 is 8+0 = 8 or Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or

excess b D-1 For pos, neg, and 0, x is represented by b D-1 + x. Ex. Let b=2 and D=4. Then the excess 8 (2 4-1 ) representation for 0 is 8+0 = 8 or Ex. Let b=2 and D=4. Then excess 8 for -1 is 8 – 1 = 7 or

2’s complement vs. excess b D-1 In 2’s, positives start with 0; in excess, positives start with 1. Both have one zero (positive). Remaining bits are the same.

Summary of methods for representing signed ints. 1000=-8| 0000 unused

BINARY ARITHMETIC Signed magnitude 1’s complement 2’s complement Excess K (biased)

BINARY ARITHMETIC Signed magnitude

Addition w/ signed magnitude algorithm For A - B, change the sign of B and perform addition of A + (-B) (as in the next step) For A + B: if (A sign ==B sign ) then{ R = |A| + |B|; R sign = A sign ; } else if (|A|>|B|) then{ R = |A| - |B|; R sign = A sign ; } else if (|A|==|B|) then{ R = 0; R sign = 0; } else{ R = |B| - |A|; R sign = B sign ; } Complicated?

BINARY ARITHMETIC 2’s complement

Representing numbers (ints) using 2’s complement Fixed, finite number of bits. bitsIntelsigned 8sbyte sword sdword sqword In general, for k bits, the signed range is [-2 k k-1 -1]. So where does the extra negative value come from?

Representing numbers (ints) Fixed, finite number of bits. bitsIntelsigned 8sbyte sword sdword sqword In general, for k bits, the signed range is [-2 k k-1 -1]. So where does the extra negative value come from?

Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. – Then the msb (bit 7) is the sign bit. If this bit is 0, then this is a positive number; if this bit is 1, then this is a negative number. – Addition of 2 positive numbers. – Ex = 98

Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. – Addition of a negative to a positive. – What are the values of these 2 terms? -88 and = 34

So how can we perform subtraction?

Addition of 2’s complement binary numbers Consider 8-bit 2’s complement binary numbers. Subtraction is nothing but addition of the 2’s complement. – Ex. 58 – 40 = 58 + (-40) = 18 discard carry

Carry vs. overflow

Addition of 2’s complement binary numbers Carry vs. overflow when adding A + B – If A and B are of opposite sign, then overflow cannot occur. – If A and B are of the same sign but the result is of the opposite sign, then overflow has occurred (and the answer is therefore incorrect). Overflow occurs iff the carry into the sign bit differs from the carry out of the sign bit.

Addition of 2’s complement binary numbers class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); } #include int main ( int argc, char* argv[] ) { char A = 127; char B = 127; char result = (char)(A + B); printf( "A + B = %d \n", result ); return 0; } Result = -2 in both Java (left) and C++ (right). Why?

Addition of 2’s complement binary numbers class test { public static void main ( String args[] ) { byte A = 127; byte B = 127; byte result = (byte)(A + B); System.out.println( "A + B = " + result ); } Result = -2 in both Java and C++. Why? What’s 127 as a 2’s complement binary number? What is ? Flip the bits: Then add 1: This is -2.

BINARY ARITHMETIC 1’s complement

Addition with 1’s complement Note: 1’s complement has two 0’s! 1’s complement addition is tricky (end-around-carry).

8-bit 1’s complement addition Ex. Let X = A8 16 and Y = Calculate Y - X using 1’s complement.

8-bit 1’s complement addition Ex. Let X = A8 16 and Y = Calculate Y - X using 1’s complement. Y = = X = = ~X = (Note: C=0 out of msb.) Y - X = = -34 (base 10)

8-bit 1’s complement addition Ex. Let X = A8 16 and Y = Calculate X - Y using 1’s complement.

8-bit 1’s complement addition Ex. Let X = A8 16 and Y = Calculate X - Y using 1’s complement. X = = Y = = ~Y = (Note: C=1 out of msb.) X - Y = = 34 (base 10) end around carry

BINARY ARITHMETIC Excess K (biased)

Binary arithmetic and Excess K (biased) Method: Simply add and then flip the sign bit > flip sign -> > flip sign -> > flip sign -> > toggle sign -> 0010 (Not used for integer arithmetic but employed in IEEE 754 floating point standard.)