靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.

Slides:



Advertisements
Similar presentations
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
Advertisements

COE 202: Digital Logic Design Signed Numbers
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Assembly Language and Computer Architecture Using C++ and Java
Chapter 4 Operations on Bits
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
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.
Number Representation Rizwan Rehman, CCS, DU. Convert a number from decimal to binary notation and vice versa. Understand the different representations.
Operations on data CHAPTER 4.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
Chapter 3 Data Representation part2 Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
The Binary Number System
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
© 2009 Pearson Education, Upper Saddle River, NJ All Rights ReservedFloyd, Digital Fundamentals, 10 th ed Digital Fundamentals Tenth Edition Floyd.
Simple Data Type Representation and conversion of numbers
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Computer Arithmetic Nizamettin AYDIN
MICRO OPERATIONS Department of Computer Engineering, M.S.P.V.L. Polytechnic College, Pavoorchatram.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Chapter 2 Data and Number Representations
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Foundations of Computer Science Chapter 4 Operations on Bits.
ECEG-3202: Computer Architecture and Organization, Dept of ECE, AAU 1 Floating-Point Arithmetic Operations.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
Number Representation
Operations on Bits Arithmetic Operations Logic Operations
©Brooks/Cole, 2003 Chapter 4 Operations on Bits. ©Brooks/Cole, 2003 Apply arithmetic operations on bits when the integer is represented in two’s complement.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Arithmetic and Logic Operations Patt and Patel Ch. 2 & 3.
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 Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
1 Ethics of Computing MONT 113G, Spring 2012 Session 4 Binary Addition.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
09/03/20161 Information Representation Two’s Complement & Binary Arithmetic.
Computer Organization 1 Data Representation Negative Integers.
Lecture No. 4 Computer Logic Design. Negative Number Representation 3 Options –Sign-magnitude –One’s Complement –Two’s Complement  used in computers.
Materials on the Exam Introduction Data Representation in Computer Systems Boolean Algebra Digital Logic MARIE: An Introduction to a Simple Computer Until.
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 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
David Kauchak CS 52 – Spring 2017
Negative Numbers and Subtraction
A brief comparison of integer and double representation
Chapter 4 Operations on Bits.
Boolean Algebra, Bitwise Operations
A Level Computing Component 2
Binary – Octal - Hexadecimal
Data Representation Data Types Complements Fixed Point 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.
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Chapter3 Fixed Point Representation
GCSE COMPUTER SCIENCE Topic 3 - Data 3.3 Logical and Arithmetic Shifts.
Chapter 1 (Part c) Digital Systems and Binary Numbers
OBJECTIVES After reading this chapter, the reader should be able to :
1.6) Storing Integer: 1.7) storing fraction:
Chapter 1 Introduction.
Two’s Complement & Binary Arithmetic
Presentation transcript:

靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱

Operations on Bits

Operations on bits You can perform arithmetic or logical operations on bits

4.1 ARITHMETIC OPERATIONS

Number of 1s ------------ Adding bits Most computers use the two’s complement method of integer representation. Adding numbers in two’s complement is like adding the numbers in decimal, if there is a carry, it is added to the next column If there is a carry after addition of the leftmost digits, the carry is discarded Number of 1s ------------ None One Two Three Result ------------ 1 Carry ------------ 1 1+1=102 1+1+1=112

Rule of Adding Integers in Two’s Complement Note: Rule of Adding Integers in Two’s Complement Add 2 bits and propagate the carry to the next column. If there is a final carry after the leftmost column addition, discard it.

Example 1 Add two numbers in two’s complement representation: (+17) + (+22)  (+39) Solution Carry 1 0 0 0 1 0 0 0 1 + 0 0 0 1 0 1 1 0 ---------------------------------- Result 0 0 1 0 0 1 1 1  39

Example 2 Add two numbers in two’s complement representation: (+24) + (-17)  (+7) Solution Carry 1 1 1 1 1 0 0 0 1 1 0 0 0 + 1 1 1 0 1 1 1 1 ---------------------------------- Result 0 0 0 0 0 1 1 1  +7

Example 3 Add two numbers in two’s complement representation: (-35) + (+20)  (-15) Solution Carry 1 1 1 1 1 0 1 1 1 0 1 + 0 0 0 1 0 1 0 0 ---------------------------------- Result 1 1 1 1 0 0 0 1  -15

Example 4 Add two numbers in two’s complement representation: (+127) + (+3)  (+130) Solution Carry 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 + 0 0 0 0 0 0 1 1 ---------------------------------- Result 1 0 0 0 0 0 1 0  -126 (Error) An overflow has occurred.

Overflow The term overflow describes a condition in which a number is not within the range defined by the bit allocation For Example 4, the range is -28-1 to +28-1-1, which is -128 to 127. The result of the addition (130) is not in this range

Range of numbers in two’s complement representation Note: Range of numbers in two’s complement representation - (2N-1) ---------- 0 ----------- +(2N-1 –1)

Two’s complement numbers visualization 1111 1111 0000 0000 1100 0000 0011 1111 1000 0000 0111 1111

Note: When you do arithmetic operations on numbers in a computer, remember that each number and the result should be in the range defined by the bit allocation.

Subtraction in two’s complement To subtract in two’s complement, just negate the number to be subtracted and perform the addition.

Example 5 Subtract 62 from 101 in two’s complement: (+101) - (+62)  (+101) + (-62) Solution 62 = 0011 1110 -62 = 1100 0010 Carry 1 1 0 1 1 0 0 1 0 1 + 1 1 0 0 0 0 1 0 ---------------------------------- Result 0 0 1 0 0 1 1 1  39 The leftmost carry is discarded.

Arithmetic on floating-point number The steps are as follows: Check the signs If the signs are the same, add the numbers and assign the sign to the result If the signs are different, compare the absolute values, subtract the smaller from the larger, and use the sign of the larger for the result Move the decimal points to make the exponents the same. Add or subtract the mantissas Normalize the result before storing in memory Check for any overflow

Example 6 Add two floats: 0 10000100 10110000000000000000000 0 10000010 01100000000000000000000 Solution The exponents are 5 and 3. The numbers are: +25 x 1.1011 and +23 x 1.011 Make the exponents the same. (+25 x 1.1011)+ (+25 x 0.01011)  +25 x 10.00001 After normalization +26 x 1.000001, which is stored as: 0 10000101 000001000000000000000000

4.2 LOGICAL OPERATIONS

Unary and binary operations Logical operation on bits can be unary (one input) or binary (two inputs)

Logical operations

Truth tables 0: False 1: True

NOT operator The unary NOT operator inverts its input.

Example 7 Use the NOT operator on the bit pattern 10011000 Solution Target 1 0 0 1 1 0 0 0 NOT ------------------ Result 0 1 1 0 0 1 1 1

AND operator The result of the binary AND operation is true only if both inputs are true.

Example 8 Use the AND operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 AND 0 0 1 1 0 1 0 1 ------------------ Result 0 0 0 1 0 0 0 0

Inherent rule of the AND operator If a bit in one input is 0, you need not check the corresponding bit in the other input  the result is always 0

OR operator The result of the binary OR operation is false only if both inputs are false.

Example 9 Use the OR operator on bit patterns 10011000 and 00110101 Solution Target 1 0 0 1 1 0 0 0 OR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 1 1 1 0 1

Inherent rule of the OR operator If a bit in one input is 1, you need not check the corresponding bit in the other input  the result is always 1

XOR (互斥) operator The result of the binary XOR operation is false only if both input are the same.

Use the XOR operator on bit patterns 10011000 and 00110101. Example 10 Use the XOR operator on bit patterns 10011000 and 00110101. Solution Target 1 0 0 1 1 0 0 0 XOR 0 0 1 1 0 1 0 1 ------------------ Result 1 0 1 0 1 1 0 1 152 XOR 53 = 173

Inherent rule of the XOR operator If a bit in one input is 1, the result is the inverse of the corresponding bit in the other input.

AND, OR, XOR

Mask A mask is a bit pattern that is applied to a target bit pattern to achieve a specific result.

Unsetting specific bits (Force to 0)

Example 11 Use a mask to unset (clear) the 3 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 00011111. Target 1 0 1 0 0 1 1 0 AND Mask 0 0 0 1 1 1 1 1 ------------------ Result 0 0 0 0 0 1 1 0

Solution on the next slide. Example 12 Imagine a power plant that pumps water to a city using eight pumps. The state of the pumps (on or off) can be represented by an 8-bit pattern. For example, the pattern 11000111 shows that pumps 1 to 3 (from the right), 7 and 8 are on while pumps 4, 5, and 6 are off. Now assume we want to shut down pump 7. How can a mask show this situation? Solution on the next slide.

Solution Use the mask 10111111 to AND with the target pattern. The only 0 bit (bit 7) in the mask turns off the seventh bit in the target. Target 1 1 0 0 0 1 1 1 AND Mask 1 0 1 1 1 1 1 1 ------------------ Result 1 0 0 0 0 1 1 1

Setting specific bits (Force to 1)

Example 13 Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 11111000. Target 1 0 1 0 0 1 1 0 OR Mask 1 1 1 1 1 0 0 0 ------------------ Result 1 1 1 1 1 1 1 0

Example 14 Using the power plant example, how can you use a mask to turn on pump 6? Solution Use the mask 00100000. Target 1 0 0 0 0 1 1 1 OR Mask 0 0 1 0 0 0 0 0 ------------------ Result 1 0 1 0 0 1 1 1

Flipping specific bits To change the value of specific bits from 0s to 1s, and vice versa

Example 15 Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution Target 1 0 1 0 0 1 1 0 XOR Mask 1 1 1 1 1 0 0 0 ------------------ Result 0 1 0 1 1 1 1 0

4.3 SHIFT OPERATIONS

Shift operations A bit pattern can be shifted to the right or to the left. The right-shift operation discards the rightmost bit, shifts every bit to the right, and inserts 0 as the leftmost bit.

Example 16 Show how you can divide or multiply a number by 2 using shift operations. Solution If a bit pattern represents an unsigned number, a right-shift operation divides the number by two. The pattern 00111011 represents 59. When you shift the number to the right, you get 00011101, which is 29. If you shift the original number to the left, you get 01110110, which is 118.

Example 17 Use a combination of logical and shift operations to find the truth value (0 or 1) of the fourth bit (from the right). Solution Use the mask 00001000 to AND with the target to keep the fourth bit and clear the other bits. Continued on the next slide

Solution (continued) Target a b c d e f g h AND Mask 0 0 0 0 1 0 0 0 ------------------ Result 0 0 0 0 e 0 0 0 Shift the new pattern three times to the right 0000e000  00000e00  000000e0  0000000e Now it is easy to test the value of the new pattern as an unsigned integer. If the value is 1, the original bit was 1; otherwise the original bit was 0.

OBJECTIVES After reading this chapter, the reader should be able to: Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits. Understand the applications of logical operations using masks. Understand the shift operations on numbers and how a number can be multiplied or divided by powers of two using shift operations.