Chapter 4 Operations on Bits.

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.
Major Numeric Data Types Unsigned Integers Signed Integer Alphanumeric Data – ASCII & UNICODE Floating Point Numbers.
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
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
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.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter3 Fixed Point Representation Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
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.
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
Floating Point Arithmetic
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.
Cougar Time. Adding Negative Numbers  What are the two rules for adding integers?  Same Signs = Add and keep the sign  Different Signs = Find the absolute.
07/12/ Data Representation Two’s Complement & Binary Arithmetic.
Digital Fundamentals Tenth Edition Floyd Chapter 2 © 2008 Pearson Education.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Number Representation and Arithmetic Circuits
©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.
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 Integer Representations V1.0 (22/10/2005). 2 Integer Representations  Unsigned integer  Signed integer  Sign and magnitude  Complements  One’s.
COSC2410: LAB 2 BINARY ARITHMETIC SIGNED NUMBERS FLOATING POINT REPRESENTATION BOOLEAN ALGEBRA 1.
973cs111_add_posneg.ppt Integers Whole numbers Do NOT contain decimal points (as in money) 43,689 is an integer 43, is NOT an integer (it is floating.
Representing Positive and Negative Numbers
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Floating Point Representations
Unit 1 Introduction Number Systems and Conversion.
David Kauchak CS 52 – Spring 2017
Prof. Sin-Min Lee Department of Computer Science
Negative Numbers and Subtraction
Boolean Algebra, Bitwise Operations
A Level Computing Component 2
Data Representation Data Types Complements Fixed Point Representation
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.
Computer Organization and Design
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Unit 18: Computational Thinking
Number Representation
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Binary & Hex Review.
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:

Chapter 4 Operations on Bits

Figure 4-1 Operations on bits

4.1 ARITHMETIC OPERATIONS

Arithmetic Operation on Integer All arithmetic operation (addition, subtraction, multiplication and division) can be applied to integers. However we focus on addition and subtraction. Because multiplication is repetitive addition and division is repetitive subtraction. Addition and subtraction can be applied to all integer representation but we interested in two’s complement representation.

Adding bits Bits ------------ 0+0 0+1 1 1+0 1+1 1 1+1+1=? 1+1+0=? Result ------------ 1 Carry ------------ 1 1+1+1=? 1+1+0=? 1+0+0+?

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 Overflow in binary addition occurs when the result of addition can not be stored within the range defined by the allocation. For two’s complement representation the addition result should be in the range between -2N-1 and 2N-1-1 where N is number of bits allocation

Why did we get wrong answer? Figure 4-2 Why did we get wrong answer? Number Visualization

Subtraction in two’s complement In two’s complement representation there is no difference between addition and subtraction. To subtract “negate” (using two’s complement) the subtracted number and add. Would overflow occurs in subtraction? How?

Example 5 Subtract 62 from 101 in two’s complement: (+101) - (+62)  (+101) + (-62) Solution 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 Operation on Floating-Point Number Check sign If the sign is positive, add numbers. If the sign is negative, compare the absolute values, subtract the smaller from the larger, attach the sign of the larger. Move the decimal point to make the exponent the same. Add or subtract Normalize the result

After normalization +26 x 1.000001, which is stored as: Example 6 Add two floats: 0 10000100 10110000000000000000000 0 10000010 01100000000000000000000 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

Logical Operation In Computer, you can interpret 0 as logical value false and 1 as logical value true. Logical operation can accept 1 or 2 bits to create only 1 bits. If operation is applied to only one input, it is a unary operation. If operation is applied to 2 inputs, it is a binary operation.

Unary and binary operations Figure 4-3 Unary and binary operations

Figure 4-4 Logical operations

Figure 4-5 Truth tables

NOT Operator NOT Operator has one input. NOT Operator inverts the bits. NOT Symbol Input x Output NOT x NOT

Figure 4-6 NOT operator

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 AND operator is a binary operator, it takes two inputs and create one output AND Symbol x AND AND Output y

Figure 4-7 AND operator

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 Figure 4-8 Inherent rule of the AND operator

OR Operator OR operator is a binary operator, it takes two inputs and create one output OR Symbol x OR Output y

Figure 4-9 OR operator

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 Figure 4-10 Inherent rule of the OR operator

XOR Operator XOR operator is a binary operator, it takes two inputs and create one output OR Symbol x XOR Output y

Figure 4-11 XOR operator

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

Inherent rule of the XOR operator Figure 4-12 Inherent rule of the XOR operator

Applications of Binary Operation Logical binary operations can be used to modify a bit pattern. They could be used to set, unset or invert specific bits. The Mask is the a bit pattern used to modify another bit pattern. A bit pattern can be ANDed, ORed or XORed with the Mask

Figure 4-13 Mask

Unsetting Specific Bits AND operator can be used to unset (force to 0) specific bits in a bit pattern. The rule for constructing an unsetting mask are: To unset a bit in the target, use 0 for the corresponding bit in the mask. To leave a bit in the target unchanged, use 1 for the corresponding bit in the mask

Example of unsetting specific bits Figure 4-14 Example of unsetting specific bits

Example 11 Use a mask to unset (clear) the 5 leftmost bits of a pattern. Test the mask with the pattern 10100110. Solution The mask is 00000111. Target 1 0 1 0 0 1 1 0 AND Mask 0 0 0 0 0 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 pump 7 shuts down. 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 OR operator can be used to set (force to 1) specific bits in a bit pattern. The rule for constructing an unsetting mask are: To set a bit in the target, use 1 for the corresponding bit in the mask. To leave a bit in the target unchanged, use 0 for the corresponding bit in the mask

Example of setting specific bits Figure 4-15 Example of setting specific bits

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 show that pump 6 is now turned on? 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 (inverting) Specific Bits XOR operator can be used to flipping specific bits (changing 0 to 1 and 1 to 0) in a bit pattern. The rule for constructing an unsetting mask are: flip a bit in the target, use 1 for the corresponding bit in the mask. To leave a bit in the target unchanged, use 0 for the corresponding bit in the mask

Example of flipping specific bits Figure 4-16 Example of flipping specific bits

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 Operation A bit pattern can be shifted to the left or the right. The right-shift operation discards the rightmost bit every shift and insert 0 to the leftmost bit. The left-shift operation discards the leftmost bit every shift and insert 0 to the rightmost bit. Note: Shifting may change the sign of the number.

Figure 4-17 Shift operations

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 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 rest of the 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.