King Fahd University of Petroleum and Minerals

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

Data Representation COE 202 Digital Logic Design Dr. Aiman El-Maleh
Addition and Subtraction. Outline Arithmetic Operations (Section 1.2) – Addition – Subtraction – Multiplication Complements (Section 1.5) – 1’s complement.
COE 202: Digital Logic Design Signed Numbers
ECE 331 – Digital System Design
Data Representation Computer Organization &
CS 151 Digital Systems Design Lecture 3 More Number Systems.
Data Representation COE 205
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
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 =
CSCE 211: Digital Logic Design Chin-Tser Huang University of South Carolina.
Data Representation ICS 233
ECE 331 – Digital System Design
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
ENGIN112 L3: More Number Systems September 8, 2003 ENGIN 112 Intro to Electrical and Computer Engineering Lecture 3 More Number Systems.
Number Systems Lecture 02.
1 Lecture 2: Number Systems Binary numbers Base conversion Arithmetic Number systems  Sign and magnitude  Ones-complement  Twos-complement Binary-coded.
CSC 221 Computer Organization and Assembly Language
Arithmetic for Computers
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Data Representation – Binary Numbers
Computers Organization & Assembly Language
#1 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
ECEN2102 Digital Logic Design Lecture 1 Numbers Systems Abdullah Said Alkalbani University of Buraimi.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
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.
Lecture 4 Last Lecture –Positional Numbering Systems –Converting Between Bases Today’s Topics –Signed Integer Representation Signed magnitude One’s complement.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
ECE 301 – Digital Electronics Unsigned and Signed Numbers, Binary Arithmetic of Signed Numbers, and Binary Codes (Lecture #2)
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.
1 IT 231, CMPE 331 Digital Logic Design Week 2 Number systems and arithmetic.
Operations on Bits Arithmetic Operations Logic Operations
Computer Math CPS120 Introduction to Computer Science Lecture 4.
AEEE2031 Data Representation and Numbering Systems.
ECE 301 – Digital Electronics Representation of Negative Numbers, Binary Arithmetic of Negative Numbers, and Binary Codes (Lecture #11) The slides included.
Number Systems & Operations
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=
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.
IT1004: Data Representation and Organization Negative number representation.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
ECE DIGITAL LOGIC LECTURE 3: DIGITAL COMPUTER AND NUMBER SYSTEMS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2016, 01/19/2016.
1 CS 151 : Digital Design Chapter 4: Arithmetic Functions and Circuits 4-3 : Binary Subtraction.
ECE DIGITAL LOGIC LECTURE 15: COMBINATIONAL CIRCUITS Assistant Prof. Fareena Saqib Florida Institute of Technology Fall 2015, 10/20/2015.
Integer Operations Computer Organization and Assembly Language: Module 5.
Data Representation COE 301 Computer Organization Dr. Muhamed Mudawar
Lecture No. 4 Computer Logic Design. Negative Number Representation 3 Options –Sign-magnitude –One’s Complement –Two’s Complement  used in computers.
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:
Data Representation COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum.
Data Representation COE 308 Computer Architecture
Design of Digital Circuits Reading: Binary Numbers
Data Representation ICS 233
Lec 3: Data Representation
Data Representation.
Data Representation Binary Numbers Binary Addition
CHAPTER 1 INTRODUCTION NUMBER SYSTEMS AND CONVERSION
Data Representation COE 301 Computer Organization
King Fahd University of Petroleum and Minerals
Data Representation Data Types Complements Fixed Point Representation
King Fahd University of Petroleum and Minerals
Data Representation ICS 233
ECE 331 – Digital System Design
Data Representation COE 308 Computer Architecture
Presentation transcript:

King Fahd University of Petroleum and Minerals Binary Arithmetic COE 202 & EE 200 Digital Logic Design Prof. Muhamed Mudawar King Fahd University of Petroleum and Minerals

Presentation Outline Binary and Hexadecimal Addition BCD Addition Binary and Hexadecimal subtraction Binary Multiplication Signed Numbers and Complement Notation Carry and Overflow

Single Bit Binary Addition Given two binary digits (X and Y) and a carry in, we get the sum (S) and the carry out (C) Carry in = 0 Carry in = 1 X + Y C S + 0 0 0 + 1 0 1 1 + 0 0 1 1 + 1 1 0 1 X + Y C S 1 + 0 0 1 1 + 1 1 0 1 + 0 1 0 1 + 1 1 1

Multiple Bit Binary Addition Start with the least significant bit (rightmost bit) Add each pair of bits Include the carry in the addition, if present 1 carry 1 1 1 1 + (54) (29) 1 2 3 4 bit position: 5 6 7 1 1 1 1 (83)

Hexadecimal Addition 1 9 C 3 7 2 8 6 5 1 E 4 B + A F C D B Start with the least significant hexadecimal digits Let Sum = summation of two hex digits If Sum is greater than or equal to 16 Sum = Sum – 16 and Carry = 1 Example: 1 carry 1 1 9 C 3 7 2 8 6 5 1 E 4 B + 5 + B = 5 + 11 = 16 Since Sum ≥ 16 Sum = 16 – 16 = 0 Carry = 1 A F C D B

Single Digit BCD Addition We use binary arithmetic to add the BCD digits Since the result is more than 9, it must use 2 digits To correct the digit, add 6 to the digit sum Eight Plus 5 is 13 (>9) 8 + 5 13 1000 + 0101 1101 8 + 5 13 1000 + 0101 1101 + 0110 0011 0001 0011 Eight Plus 5 is 13 (>9) so add 6 3 and carry Final answer in BCD

Multiple Digit BCD Addition Add 2905 + 1897 in BCD Showing carries and digit corrections carry +1 +1 +1 0010 0001 0100 1001 1000 10010 0110 0000 1001 1010 0110 0101 0111 1100 0110 0010 + digit correction Final answer: 2905 + 1897 = 4802

Single Bit Binary Subtraction Given two binary digits (X and Y), and a borrow in we get the difference (D) and the borrow out (B) shown as -1 Borrow in = 0 Borrow in = -1 X – Y B D – 0 0 0 – 1 -1 1 1 – 0 0 1 1 – 1 0 0 -1 X – Y B D -1 – 0 -1 1 -1 – 1 -1 0 -1 1 – 0 0 0 -1 1 – 1 -1 1

Multiple Bit Binary Subtraction Start with the least significant bit (rightmost bit) Subtract each pair of bits Include the borrow in the subtraction, if present borrow -1 -1 -1 1 – (54) (29) 1 2 3 4 bit position: 5 6 7 1 1 1 (25)

Hexadecimal Subtraction Start with the least significant hexadecimal digits Let Difference = subtraction of two hex digits If Difference is negative Difference = 16 + Difference and Borrow = -1 Example: A -1 borrow A -1 4 -1 9 C 3 7 2 8 6 5 1 E 4 B - Since 5 < B, Difference < 0 Difference = 16+5–11 = 10 Borrow = -1 8 8 1 1

Binary Multiplication Binary Multiplication table is simple: 0×0=0, 0×1=0, 1×0=0, 1×1=1 Multiplicand 11002 = 12 Multiplier × 11012 = 13 1100 0000 Product 100111002 = 156 n-bit multiplicand × n-bit multiplier = 2n-bit product Accomplished via shifting and addition Binary multiplication is easy 0 × multiplicand = 0 1 × multiplicand = multiplicand

Registers Registers are fast storage devices used inside processors Used to store computation results of a running program A Register consists of a fixed number n of storage bits The register size n is typically a power of 2 (8, 16, 32, 64) Numbers stored in registers are either unsigned or signed Byte 8 bits 16 bits 32 bits 64 bits Word Double Word Quad Word Register Sizes The byte size is equal to 8 bits, but the word size can vary from one computer to another

Signed Numbers Several ways to represent a signed number Sign-Magnitude 1's complement 2's complement Divide the range of values into 2 equal parts First part corresponds to the positive numbers (≥ 0) Second part correspond to the negative numbers (< 0) The 2's complement representation is widely used Has many advantages over other representations

Sign-Magnitude Representation n-bit register Sign Bit bit n-2 . . . bit 2 bit 1 bit Magnitude = n – 1 bits Independent representation of the sign and magnitude Leftmost bit is the sign bit: 0 is positive and 1 is negative Using n bits, largest represented magnitude = 2n-1 – 1 Sign-magnitude representation of +45 using 8-bit register Sign-magnitude representation of -45 using 8-bit register 1 1

Properties of Sign-Magnitude Two representations for zero: +0 and -0 Symmetric range of represented values: For n-bit register, range is from -(2n-1 – 1) to +(2n-1 – 1) For example using 8-bit register, range is -127 to +127 Hard to implement addition and subtraction Sign and magnitude parts have to processed independently Sign bit should be examined to determine addition or subtraction Addition is converted into subtraction when adding numbers of different signs Need a different circuit to perform addition and subtraction Increases the cost of the logic circuit

1’s Complement Representation Given a binary number N The 1’s complement of N is obtained by reversing each bit in N (0 becomes 1, and 1 becomes 0) Example: 1’s complement of (01101001)2 = (10010110)2 If N consists of n bits then 1’s complement of N = (2n – 1) – N (2n – 1) is a binary number represented by n 1’s Example: if n = 8 then (28 – 1) = 255 = (11111111)2 1’s complement of (01101001)2 = (11111111)2 – (01101001)2 = (10010110)2

2’s Complement Representation Almost all computers today use 2’s complement to represent signed integers A simple definition for 2’s complement: Given a binary number N The 2’s complement of N = 1’s complement of N + 1 Example: 2’s complement of (01101001)2 = (10010110)2 + 1 = (10010111)2 If N consists of n bits then 2’s complement of N = 2n – N

Computing the 2's Complement starting value 001001002 = +36 step1: reverse the bits (1's complement) 110110112 step 2: add 1 to the value from step 1 + 12 sum = 2's complement representation 110111002 = -36 2’s complement of 110111002 (-36) = 001000112 + 1 = 001001002 = +36 The 2’s complement of the 2’s complement of N is equal to N Another way to obtain the 2's complement: Start at the least significant 1 Leave all the 0s to its right unchanged Complement all the bits to its left Binary Value = 00100 1 00 2's Complement = 11011 1 00 least significant 1

Unsigned and Signed Value Positive numbers Signed value = Unsigned value Negative numbers Signed value = Unsigned value – 2n n = number of bits Negative weight for MSB Another way to obtain the signed value is to assign a negative weight to most-significant bit = -128 + 32 + 16 + 4 = -76 8-bit Binary value Unsigned Signed 00000000 00000001 1 +1 00000010 2 +2 . . . 01111110 126 +126 01111111 127 +127 10000000 128 -128 10000001 129 -127 11111110 254 -2 11111111 255 -1 1 -128 64 32 16 8 4 2

Properties of the 2’s Complement The 2’s complement of N is the negative of N The sum of N and 2’s complement of N must be zero The final carry is ignored Consider the 8-bit number N = 001011002 = +44 -44 = 2’s complement of N = 110101002 001011002 + 110101002 = 1 000000002 (8-bit sum is 0) In general: Sum of N + 2’s complement of N = 2n where 2n is the final carry (1 followed by n 0’s) There is only one zero: 2’s complement of 0 = 0 Ignore final carry

Ranges of Unsigned/Signed Integers For n-bit unsigned integers: Range is 0 to (2n – 1) For n-bit signed integers: Range is -2n–1 to (2n–1 – 1) Positive range: 0 to (2n–1 – 1) Negative range: -2n–1 to -1 Storage Size Unsigned Range Signed Range 8 bits (byte) 0 to (28 – 1) = 255 -27 = -128 to (27 – 1) = +127 16 bits 0 to (216 – 1) = 65,535 -215 = -32,768 to (215 – 1) = +32,767 32 bits 0 to (232 – 1) = 4,294,967,295 -231 = -2,147,483,648 to (231 – 1) = +2,147,483,647 64 bits 0 to (264 – 1) = 18,446,744,073,709,551,615 -263 = -9,223,372,036,854,775,808 to (263 – 1) = +9,223,372,036,854,775,807

Sign Extension Step 1: Move the number into the lower-significant bits Step 2: Fill all the remaining higher bits with the sign bit This will ensure the correctness of the signed value Examples Sign-Extend 101100112 to 16 bits Sign-Extend 011000102 to 16 bits Infinite 0’s can be added to the left of a positive number Infinite 1’s can be added to the left of a negative number 101100112 = -77 11111111 10110011 = -77 011000102 = +98 00000000 01100010 = +98

Subtraction with 2’s Complement When subtracting A – B, convert B to its 2's complement Add A to (2’s complement of B) 0 1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 0 1 1 1 0 1 0 1 1 0 0 0 1 1 0 (2's complement) 0 0 0 1 0 0 1 1 0 0 0 1 0 0 1 1 (same result) Final carry is ignored, because Negative number is sign-extended with 1's You can imagine infinite 1's to the left of a negative number Adding the carry to the extended 1's produces extended zeros borrow: -1 -1 -1 carry: 1 1 1 1 – +

Carry and Overflow Carry is important when … Adding or subtracting unsigned integers Indicates that the unsigned sum is out of range Either < 0 or >maximum unsigned n-bit value Overflow is important when … Adding or subtracting signed integers Indicates that the signed sum is out of range Overflow occurs when Adding two positive numbers and the sum is negative Adding two negative numbers and the sum is positive Can happen because of the fixed number of sum bits

Carry and Overflow Examples We can have carry without overflow and vice-versa Four cases are possible (Examples are 8-bit numbers) 1 + 15 8 23 Carry = 0 Overflow = 0 1 + 15 248 (-8) 7 Carry = 1 Overflow = 0 1 + 79 64 143 (-113) Carry = 0 Overflow = 1 1 + 218 (-38) 157 (-99) 119 Carry = 1 Overflow = 1

Range, Carry, Borrow, and Overflow Unsigned Integers: n-bit representation Signed Integers: n-bit 2's complement representation Borrow = -1 Subtraction Numbers < min Carry = 1 Addition Numbers > max Finite Set of Unsigned Integers min = 0 max = 2n–1 Negative Overflow Numbers < min Positive Overflow Numbers > max Finite Set of Signed Integers min = -2n-1 max = 2n-1–1