Foundations of Computer Science 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.
電腦的基本單位 類比訊號 (analog signal) 指的是連續的訊號
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 =
電腦的基本單位 類比訊號 (analog signal) 指的是連續的訊號 數位訊號 (digital signal) 指的是以預先定義的符號表示不連續 的訊號 one bit 8 bits=one byte 電腦裡的所有資料,包括文 字、數據、影像、音訊、視 訊,都是用二進位來表示的。
電腦的基本單位 類比訊號 (analog signal) 指的是連續的訊號
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Chapter 3 Number Representation
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.
Chapter 4 Operations on Bits. Apply arithmetic operations on bits when the integer is represented in two’s complement. Apply logical operations on bits.
Simple Data Type Representation and conversion of numbers
Chapter 3 Number Representation. Convert a number from decimal 、 hexadecimal,octal to binary notation and vice versa. Understand the different representations.
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
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.
Chapter 1 Data Storage(3) Yonsei University 1 st Semester, 2015 Sanghyun Park.
Calculating Two’s Complement. The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of.
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 Representation of Data within the Computer Oct., 1999(Revised 2001 Oct)
Number Systems Revision of conversations What is a register Addition Complementation.
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.
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.
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.
{ Binary “There are 10 types of people in the world: Those who understand binary, and those who don't.”
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.
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
David Kauchak CS 52 – Spring 2017
Chapter 4 Operations on Bits.
Boolean Algebra, Bitwise Operations
A Level Computing Component 2
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.
Number Representation
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:
Presentation transcript:

Foundations of Computer Science Chapter 4 Operations on Bits

2 Outline 4.1 Arithmetic Operations 4.2 Logical Operations 4.3 Shift Operations

3 Operations on Bits You can perform arithmetic or logical operations on bits Bit operations ( 位元運算 ) Arithmetic ( 算數 ) Logical ( 邏輯 )

4 4.1 Arithmetic Operations 4.1 Arithmetic Operations

5 Adding Bits Most computer use the two’s complement method of integer representation Adding numbers in two’s complement is like adding the number 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 Number of 1s None One Two Three Result Result Carry Carry

6 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 left most column addition, discard it.

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

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

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

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

11 Overflow ( 溢位 ) The term overflow describes a condition in which a number is not within the range defined by the bit allocation For example4, the range is to , which is -128 to 127. The result of the addition (130) is not in this range

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

13 Two’s Complement Numbers Visualization

14 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 ( 配置 )

15 Subtraction in Two's Complement To subtract in two‘s complement, just negate (two’s complement, 指使用二的補 數對數值進行變更正負號 ) the second number to be subtracted and add X – Y = X + (-Y)

16 Example Subtract 62 from 101 in two’s complement: (+101) - (+62)  (+101) + (-62) Solution Carry Result  39 The leftmost carry is discarded.

17 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

18 Example Add two floats: Solution The exponents are 5 and 3. The numbers are: +2 5 * and +2 3 * Make the exponents the same. (+2 5 * ) + (+2 5 * )  +2 5 * After normalization +2 6 * , which is stored as:

Logical Operations 4.2 Logical Operations

20 Unary and Binary Operations Logical operation on bits can be unary (one input, 單一 ) or binary (two inputs)

21 Logical Operations

22 Truth Tables ( 真值表 )

23 NOT Operator The unary NOT operator inverts ( 反轉 ) its input.

24 NOT Operator x NOT x

25 Example Use the NOT operator on the bit pattern Solution Target NOT Result

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

27 AND Operator x y x AND y

28 Example Use the AND operator on bit patterns and Solution Target AND Result

29 Inherent Rule of the AND Operator If a bit in one input is 0, you do not have to check the corresponding bit in the other input → the result is 0

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

31 OR Operator x y x OR y

32 Example Use the OR operator on bit patterns and Solution Target OR Result

33 Inherent Rule of the OR Operator If a bit in one input is 1, you do not have to check the corresponding bit in the other input → the result is 1

34 XOR Operator The result of the binary XOR operation is false only if both input are the same. XOR: eXclusive ( 獨占的 ) OR

35 XOR Operator x y x XOR y

36 Example Use the XOR operator on bit patterns and Solution Target XOR Result

37 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

38 Mask ( 遮罩 ) A mask is a bit pattern that is applied to a target bit pattern to achieve a specific result.

39 Unsetting Specific Bits Unset ( 清除 ) : force ( 強迫 ) to 0

40 Example Use a mask to unset (clear, 清除 ) the 5 leftmost bits of a pattern. Test the mask with the pattern Solution The mask is Target AND Mask Result

41 Example Imagine a power plant ( 發電廠 ) that pumps ( 幫浦 ) water to a city using eight (8) pumps. The state of the pumps (on or off) can be represented by an 8- bit pattern. For example, the pattern 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?

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

43 Setting Specific Bits Set ( 設定 ) : force ( 強迫 ) to 1

44 Example Use a mask to set the 5 leftmost bits of a pattern. Test the mask with the pattern Solution The mask is Target OR Mask Result

45 Example Using the power plant ( 發電廠 ) example, how can you use a mask to to show that pump 6 is now turned on (pump’s status )? Solution Use the mask Target OR Mask Result

46 Flipping Specific Bits To change the value of specific bits from 0s to 1s, and vice versa ( 反之亦然, 1s to 0s) Flip ( 翻轉 ) : 0 to 1, and 1 to 0

47 Example Use a mask to flip the 5 leftmost bits of a pattern. Test the mask with the pattern Solution Target XOR Mask Result

Shift Operations 4.3 Shift Operations

49 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.

50 Example 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 represents 59. When you shift the number to the right, you get , which is 29. A left-shift operation multiply ( 乘 ) the number by two. If you shift the original number to the left, you get , which is 118.

51 Example 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 to AND with the target to keep the fourth bit and clear the rest of the bits. Continued on the next slide

52 Solution (Continued) Targeta b c d e f g hAND Mask Result e Shift the new pattern three times to the right 0000e000  00000e00  e0  e 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.

53 The End