Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Data and Number Representations

Similar presentations


Presentation on theme: "Chapter 2 Data and Number Representations"— Presentation transcript:

1 Chapter 2 Data and Number Representations

2 Outline Data Representation (chapter2)
Data and Information Data Types Digital Data Representation How To Represent Different Types of Data Number Representation (chapter3) Number Systems Conversion Between Number Systems Integer Representation Floating-point Representation Operations on Bits (chapter4) Arithmetic Operations Logical Operations

3 Objectives Differentiate between data and information.
Explain how text, images, audio and video are represented in computers. Explain decimal notation, binary notation, hexadecimal notation, and octal notation. Apply conversions from one number system to another.

4 Objectives Explain how integers are stored in computers (sign-and-magnitude, one’s complement, two’s complement). Explain how the Excess system works. Explain how to represent a floating-point number in computers. Apply bit operations such as arithmetic operations, logical operations, and shift operations.

5 Part 1 Data Representation

6 Overview What’s data? Data refers to the symbols that a computer uses to represent facts (such as people, events and things) and ideas. What’s information? The words, numbers, and graphics used as the basis for human actions and decisions. The difference between data and information: Data becomes information when it is presented in a format that people can understand and use.

7 Graphics Data Information Table

8 Overview (con.) Data entered into a computer is called input. The processed information are called output. The cycle of input, process, output, and storage is called the information processing cycle. Data information

9 Data Text Number Image Audio Video
Overview (con.) There are several types of data. Data Text Number Image Audio Video The computer industry uses the term “multimedia” to define information that contains numbers, text, images, audio, and video. Note:

10 Question : How do you handle all these data types (text, number, image, audio and video)?
1234…890….

11 Solution The most efficient solution is to use a uniform representation of data. All data types from outside a computer are transformed into this uniform representation when stored in a computer and then transformed back when leaving the computer. This universal format is called a bit pattern.

12 Digital Data Representation
Digital device works with discrete, distinct data or digits, such as 1 and 0. Analog device works with continuous data. Digital Signals Analog Most computers are digital computers because the digital is a relatively simple, dependable, and adaptable technology.

13 Some important concepts
Bit– short for binary digit, is the smallest unit of data that can be stored in a computer. In the binary system, each 0 and 1 is called a bit. In a two-state on/off arrangement (such as a switch), one state can represent a 1 digit (on), the other represents a 0 digit (off). Computers use sequences of bits (bit pattern) to represent all kinds of data. Bit: binary digit 二进制位

14 Some important concepts (con.)
Bit pattern is a sequence or a string of bits. It is the responsibility of I/O devices or programs to interpret a bit pattern as a number, text, or some type of data. Byte is a kind of bit pattern. Its length is 8 bits. 1 byte = 8 bits

15 Data Representation (con.)
How to represent different types of data: Number Representation Text Representation Image Representation Audio Representation In a computer, numbers are represented using the binary system. Note:

16 A piece of text in any language is a sequence of symbols.
Text Representation A piece of text in any language is a sequence of symbols. Symbol examples: 26 uppercase letters (A, B, C … Z) 26 lowercase letters (a, b, c …z) 10 numeric characters (0, 1, 2 … 9) Others (? ; blank, newline, and tab….) In a bit pattern, the number of bits to represent a symbol depends on how many symbols are in the set. --- More symbols mean a longer bit pattern.

17 Text Representation (con.)

18 Text Representation (con.)
The relationship between the length of the bit pattern and the number of symbols is logarithmic. For example: If you need 4 symbols, the length is 2 bits. log24 =2 The forms are: 00, 01, 10 and 11 Number of symbols Bit pattern length 2 1 16 4 128 7 ……. 65,536

19

20 Text Representation (con.)
Different sets of bit patterns have been designed to represent text symbols. Each set is called a code, or code scheme. There are some common codes: ASCII Extended ASCII EBCDIC Unicode ISO 美国信息交换标准码: American Standerd code for Information Interchange 扩充的二进制编码的十进制交换码 Extended Binary Coded Decimal Interchange Code

21 ASCII Code Popular Code Schemes
Stands for American Standard Code for Information Interchange. It was developed by ANSI (American National Standards Institute). It is used on most microcomputers, many minicomputers, and some mainframe computers. This code uses 7 bits for each symbol, which means 128 (27) different symbols can be defined. ASCII is pronounced “ASK ee”.

22 Features of ASCII code :
Popular Code Schemes Features of ASCII code : Range of 7-bit pattern is from to The first pattern ( ) represents the null character. The last pattern ( ) represents the delete character. There are 31 control characters.

23 A-65 a-97

24 It uses 8-bits pattern to represent 256 characters.
Popular Code Schemes Extended ASCII Code To make the size of each pattern 1 byte (8 bits) , the ASCII bit patterns are augmented with an extra 0 at the left. The code is from to The left bit (0) is extended bit. EBCDIC Code Stands for Extended Binary Coded Decimal Interchange Code. It was commonly used in IBM mainframes. It uses 8-bits pattern to represent 256 characters. 扩充的二进制编码的十进制交换码 Extended Binary Coded Decimal Interchange Code

25 Popular Code Schemes Unicode
Although previous codes can handle English and European languages well, it cannot handle all the characters of some other languages, such as Chinese and Japanese. Unicode, which was developed to deal with such languages, uses 2 bytes (16 bits) to handle characters. ISO Code It is developed by ISO (International Organization for Standardization), which uses 4 bytes (32 bits) to handle (232) characters. 我国文字从古代的象形文字发展到今天的汉字,中间是经过多次变化和改革的;但是历代的改革几乎都是兼收并蓄的,以致使汉字越积越多。当前我国文字之多是非常惊人的,比较常用的《新华字典》就有10000多个汉字;1990年出版的《辞海》有14872个汉字;1716年编撰的《康熙字典》有47035个汉字;郭沫若生前曾根据日本朋友的估计,我国大约有60000多个汉字;1994年出版的《中华字海》有87019个汉字(其中重复字320个);近来,根据北京国安资讯设备公司汉字库公布,共收入有出处的汉字91251个汉字。其中一级适用汉字21303个,包括国家标准提出的汉字、我国古今近1200个姓氏6699个汉字和当前乡村一级的地名专用字;二级适用汉字36581个,包括古今权威字典中音、义具全的汉字和十三经、二十四史等的古籍用字;三级备用汉字33367个,包括罕用字、异形字、音义未详字等。尚不包括杂书野史和各地地方志、名人笔记等自造汉字;还有正、草、隶、篆不同写法的异体字、同义字等。单据《说文解字》中就有别体字147个。有人估计实际汉字约在10万个以上;也有人估计在120000个以上;我国究竟有多少汉字至今尚没有定论。

26 Image Image Representation Bitmap Vector Video Bitmap:
--- an image is divided into a matrix of pixels. --- each pixel is assigned a bit pattern. --- to represent a black and white image, 1 represents white pixel and 0 represents black one. --- to represent a color image, a pixel is decomposed into three colors (RGB).

27 Image Representation (con.)

28 Image Representation (con.)

29 Image Representation (con.)
For a color image, each pixel has three bit patterns: one to represent the intensity of the red color, one to represent the intensity of the green color, and one to represent the intensity of the blue color.

30 Image Representation (con.)
Vector: ---an image is decomposed into a combination of curves and lines. ---each curve or line is represented by a mathematical formula. ---For example, a line may be described by the coordinates of its endpoints, and a circle may be described by the coordinates of its center and the length of its radius. ---they use much less storage space than bitmap images, but do not look as realistic as bitmap images.

31 Image Representation (con.)

32 Image Representation (con.)
Video: ---is composed of a series of frames. A movie is a series of frames shown one after another to create the illusion of motion. ---each frame could be stored as a bitmap. ---a digital video requires tremendous storage capacity. ---Refer to chapter 15 for details about video compression.

33 ---Audio is a representation of sound or music.
Audio Representation ---Audio is a representation of sound or music. ---music, voice and sound effects can all be recorded as waveform, which is by nature analog data. ---samples of the sound are collected as periodic intervals and stored as numeric data.

34 Audio Representation (con.)
The steps to change audio data to bit patterns: The analog data is sampled. Sampling means measuring the value of the signal at equal intervals. The samples are quantized. Quantization means assigning a value to a sample. The quantized values are changed to binary patterns. The binary patterns are stored.

35 Audio Representation (con.)

36 Audio Representation (con.)

37 Number Representation
Part 2 Number Representation

38 Some essential concepts
Number system: any system of naming or representing numbers, also called number representation system or numeration system. Carry: happens when the sum or product of two or more digits equals or exceeds the base of the number system.

39 Base: the number of digits in a number system.
Some essential concepts Base: the number of digits in a number system. Position value: the value associated with each digit place, also called radix, weight or positional value. --For example: for the decimal number 125, the position values associated with the character 1,2,5 are respectively 102,101,100, therefore, (125)10=1×102+2×101+5×100.

40 Question: how do we compare these two numbers, 25. 6 and 52
Question: how do we compare these two numbers, 25.6 and 52.6, in the decimal system? (25.6)10 = 2 × × × 10-1 (52.6)10 = 5 × × × 10-1 (N)R =  Ki  Ri Ki{0,1,……,R-1} n i=-m n = number of digits of the integer - 1 m = number of digits of the decimal fraction R = base Ri = Position values

41 Different Number Systems
Binary System (Base 2): consists of 2 digits (symbols). Octal System (Base 8): consists of 8 digits. Decimal System (Base 10): consists of 10 digits. Hexadecimal System (Base 16): consists of 16 digits. 0 1 A B C D E F

42 Question: how do we tell which number system we are using?
Subscript method: (101)2 (101) 8 (101) 10 (101) 16 Binary Octal Decimal Hexadecimal 101B 101O 101D 101H Prefix method: Postfix method:

43 243 Binary System Its base is 2. It has only two digits,1 and 0.
There will be a carry when the result equals 2. Position Value Table 243

44 Binary Position Value Table
Position Power Decimal Value Binary Value 216 65536 210 1024 29 512 28 256 27 128 26 64 25 32 24 16 1 0000 23 8 1000 22 4 100 21 2 10 20 1

45 How to represent (16)10 in the 16-bit system??
Decimal Binary Binary (16-bit system) 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 1010 1011 12 1100 13 1101 14 1110 15 1111 How to represent (16)10 in the 16-bit system??

46 How computer capacity is expressed: bit by bit
The following terms are used to denote capacity: Bit: In the binary system, the binary digit (bit) –0 or 1—is the smallest unit of measurement. Byte: A group of 8 bits is called a byte, and a byte represents one character, digit, or other value. Kilobyte: A kilobyte(K, KB) is about 1000 bytes. (Actually, it's precisely 1024, that is 210bytes, but the figure is commonly rounded.)

47 How computer capacity is expressed: bit by bit
Megabyte: A megabyte (M, MB) is about 1 million bytes (220). Gigabyte: A gigabyte (G, GB) is about 1 billion bytes (230). Terabyte: A terabyte (T, TB) represents about 1 trillion bytes (240). Petabyte(250): A new measurement accommodates the huge storage capacities of modern databases—a petabyte represents about 1 million gigabytes! 实际设备的M和理论上的M有什么区别?

48 Octal System Bit Pattern Oct Digit 000 100 4 001 1 101 5 010 2 110 6
Its base is 8. It has eight digits: 0, 1, 2, 3, 4, 5, 6 and 7. There will be a carry when the result equals 8. A 3-bit pattern can be represented by an octal digit, and vice versa. Bit Pattern Oct Digit 000 100 4 001 1 101 5 010 2 110 6 011 3 111 7

49 Decimal System Nowadays, there are two dominant number systems in the world: decimal system and binary system. It has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.

50 Hexadecimal System Its base is 16. It has sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E and F. A 4-bit pattern can be represented by a hexadecimal digit, and vice versa. Bit Pattern Hex Digit 0000 1000 8 0001 1 1001 9 0010 2 1010 10 0011 3 1011 11 0100 4 1100 12 0101 5 1101 13 0110 6 1110 14 0111 7 1111 15

51 Decimal Binary Octal Hexadecimal 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 1010 12 A 1011 13 B 1100 14 C 1101 15 D 1110 16 E 1111 17 F

52 Conversion between number systems
Binary to Decimal Decimal to Binary Binary to Octal Octal to Binary Binary to Hexadecimal Hexadecimal to Binary

53 Binary to Decimal Conversion
Solution: Step 1: multiply each binary digit by its corresponding position value. Step 2: add all multiplication results together to get the decimal number. Start from 0 Binary Number Position Position Value 26 25 24 23 22 21 20 0×26 + 1×25 +0×24 + 1×23 + 1×22 + 0×21 + 1×20 = ( 45 )10 Result

54 Decimal to Binary Conversion
Solution: Step 1: divide the number by 2 and write the quotient and remainder. Step 2: use the remainder (from step 1) as the corresponding binary digit (from right to left), 1 or 0 . Step 3: check whether the quotient is 0 or not. If it is zero, skip to the Step 4; otherwise, use the quotient as the number and go back to Step 1. Step 4: stop and put all remainders together to get the binary number.

55 Example: Convert the decimal number 35 to binary.
Stop when the quotient is 0 Quotient Decimal Number 1 2 4 8 17 35 1 1 1 Remainder Binary Number :

56 Questions: What if the number has a fraction part?
How do we convert octal to decimal, and decimal to octal? How do we convert hexadecimal to decimal, and decimal to hexadecimal?

57 Binary to Octal Conversion
Solution: Step 1: Organize the pattern into groups of 3 (from right to left). Step 2: Transform each group into an octal digit. 1 7 6 3 4 4 If the leftmost bit pattern does not contain 3 digits, add extra 0s to the left.

58 Octal to Binary Conversion
Solution: Transform each octal digit into a 3-bit binary pattern. Octal Number Octal Number 001 100 010 101 110 010 Binary Number Binary Number

59 Step 2: Transform each group into a hexadecimal digit.
Binary to Hexadecimal Solution: Step 1: Organize the bit pattern into groups of 4 (from right to left). Step 2: Transform each group into a hexadecimal digit. F C E 4 If the leftmost bit pattern does not contain 3 digits, add extra 0s to the left.

60 Transform each hexadecimal digit into a 4-bit binary pattern.
Hexadecimal to Binary Solution: Transform each hexadecimal digit into a 4-bit binary pattern. Hexadecimal Number C 0010 0100 1100 Binary Number

61 Exercises ( )2 = (?)8 = (273.64)8 ( )8 = (?)2 = ( )2 ( )2 = (?)16 = (BE6.D8)16 (A7B8.C9)16 = (?)2 = ( )2

62 Integer Representation
Integers are the whole numbers, which include positive integers and negative integers. The figure below shows different integers. Integers Unsigned Signed Sign-and- Magnitude One’s Complement Two’s Complement

63 Unsigned Integers Range of unsigned integers: 0 ~ ( 2N-1 ) ---N is the number of bits the computer allocates to store an unsigned integer. ---For example: 8 -bit computer : 0 ~ 255 16-bit computer : 0 ~ 65,535 How to store an unsigned integer: Step 1: Convert the number to binary. Step 2: If the number of bits is less than N, 0s are added to the left of the binary number so that there is a total of N bits.

64 Unsigned integers in two different computers
Add zeros to the left when needed Decimal 8-bit Allocation 16-bit Allocation 7 234 258 24,760 1,245,678 overflow overflow overflow overflow Unsigned integers in two different computers Exceeds the capacity of the storage

65 Signed Integers Sign-and-Magnitude Representation The leftmost bit defines the sign of the number : 0 for positive, and 1 for negative. ---For example: in an 8-bit allocation, the leftmost bit shows the sign and the other seven bits represent the absolute value. Range of signed integers: -(2N-1-1) ~ +( 2N-1-1) ---N is the number of bits allocated to represent one sign-and-magnitude integer.

66 Note: There are two 0s in sign-and-magnitude representation: positive zero (+0) and negative zero (-0). ---For example: In an 8-bit allocation, +0   How to store a sign-and-magnitude integer: Step 1: Convert the number to binary, the sign is ignored. Step 2: If the number of bits is less than N-1, 0s are added to the left of the number so that there is a total of N-1 bits. Step 3: Add sign-bit to the left ( to make it N bits ) : if it’s positive, add 0; if negative, add 1.

67 Example: Store +7 in an 8-bit memory location, and store –258 in a 16-bit memory location using sign-and-magnitude representation . +7 Question: how about +7 in a 16-bit allocation, and -258 in an 8-bit allocation? 1 1 1 1 1 1 1 1 1 -258 1 1 1 1 1 1 1

68 Signed Integers One’s Complement Representation To represent a positive number, use the convention adopted for an unsigned integer. To represent a negative number, complement the positive number. In one’s complement, the complement of a number is obtained by changing all 0s to 1s and all 1s to 0s. The leftmost bit defines the sign of the number : 0 for positive, and 1 denotes negative. Range of signed integers: -(2N-1-1) ~ +( 2N-1-1) ---N is the number of bits allocated to represent a one’s complement integer.

69 Note: There are two 0s in one’s complement representation, too: positive zero (+0) and negative zero (-0). ---For example: In an 8-bit allocation, +0   How to store a one’s complement integer: Step 1: Convert the number to binary, the sign is ignored. Step 2: 0s are added to the left of the number to make a total of N bits. Step 3: If the sign is positive, no action is needed. If negative, every bit is complemented.

70 Example: Store +7 and -7 in an 8-bit memory location, and store –258 in a 16-bit memory location using one’s complement representation . +7 -7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -258 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

71 Note: One’s complementing means reversing all bits. If you one’s complement a positive number, you get the corresponding negative number. If you one’s complement a negative number, you get the corresponding positive number. If you one’s complement a number twice, you get the original number.

72 Signed Integers Two’s Complement : Range of signed integers: -(2N-1) ~ +( 2N-1-1) --- N is the number of bits allocated to represent a two’s complement integer. How to store a two’s complement integer: Step 1: Convert the number to binary, ignore the sign. Step 2: 0s are added to the left of the number to make a total of N bits. Step 3: If the sign is positive, no action is needed. If negative, leave all the rightmost 0s and the first 1 unchanged. Complement the rest of the bits. Two’s complement is the most common, the most important, and the most widely used representation of integers today.

73 Two’s complement =one’s complement + 1
Example: Store +7 in an 8-bit memory location, and store –40 in a 16-bit memory location using two’s complement representation . +7 1 1 1 1 1 1 -40 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note: For negative numbers: Two’s complement =one’s complement + 1

74 Note: In two’s complement representation, the leftmost bit defines the sign of the number. If it is 0, the number is positive. If it is 1, the number is negative. There is only one 0 in two’s complement: For example: in an 8-bit allocation, 0  If you two’s complement a positive number, you get the corresponding negative number. If you two’s complement a negative number, you get the corresponding positive number. If you two’s complement a number twice, you get the original number.

75 Summary of integer representation
Contents of Memory 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Unsigned 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Sign-and-Magnitude +0 +1 +2 +3 +4 +5 +6 +7 -0 -1 -2 -3 -4 -5 -6 -7 One’s Complement +0 +1 +2 +3 +4 +5 +6 +7 -7 -6 -5 -4 -3 -2 -1 -0 Two’s Complement +0 +1 +2 +3 +4 +5 +6 +7 -8 -7 -6 -5 -4 -3 -2 -1

76 Excess system In an excess conversion, a positive number, called the magic number, is used in the conversion process. The magic number is normally (2N-1) or ( 2N-1-1), where N is the bit allocation. For example, if N is 8, the magic number is either 128 (Excess_128) or 127(Excess_127). To represent a number in Excess, use the following procedure: Step 1: Add the magic number to the integer. Step 2: Change the result to binary and add 0s to make a total of N bits.

77 Example: Represent –25 in Excess 127 using an 8-bit allocation.
Step 1: Add 127 to –25 and get 102. Step 2: Change the result to binary, which is Step 3: Add one 0 to make a total of 8 bits. The representation is

78 See Decimal to binary Conversion Repetitive Multiplication Method
Floating-point Representation Fraction Part Integer Part 1 4 See Decimal to binary Conversion To convert a floating-point number to binary: Step 1: Convert the integer part to binary. Step 2: Convert the fraction part to binary. Step 3: Put decimal point between the two parts. Repetitive Multiplication Method

79 Changing fractions to binary
Multiply by 2 to get integer part Multiply 2 to gain integer part Stop when the result is 0 0.125 0.250 0.500 1.000 0.000 0 . 1 Transform the fraction 0.4 to a binary of 6 bits? 0.4 0.8 1.6 1.2 0.4 0.8 1.6 0 . 1 1 1

80 Example: transform the number 49
Example: transform the number to a binary ( 3 bits after the decimal point)? ( )10 = ( )2 110001 100 Integer Part 0.58 Fraction Part

81 (Excess representation)
Floating-point Representation Normalization: the moving of the decimal point so that there is only one ‘1’ to the left of the decimal point (1. XXXXXXXXX) . 6 digits + 26× 3 digits - 2-3× ± 2e × 1.XXXXXXX Sign (1 bit) Exponent (Excess representation) Mantissa (unsigned integer)

82 Single-precision format Double-precision format
Floating-point Representation IEEE Standard: Single-precision format 4 bytes = 32 bits sign 1 8 23 exponent mantissa 1 11 52 Double-precision format 8 bytes = 64 bits

83 Floating-point Representation
IEEE Standard:

84 Example: Show the representation of the normalized number: + 26 × 1
The sign is positive. The Excess_127 representation of the exponent is 133. You add extra 0s on the right to make it 23 bits. The number in memory is stored as:

85 Example: Interpret the following 32-bit floating-point number 1 01111100 11001100000000000000000
The sign is negative. The exponent is –3 (124 – 127). The number after normalization is ×

86 Quiz The decimal equivalent of the binary number 11.11 is .
Show the following number in 32-bit IEEE format: -2-5× Suppose the following bit pattern represents a value stored in two’s complement notation. Find the two’s complement representation of the negative of the value.

87 Part 3 Operations on Bits

88 Bit Operations Arithmetic Logical Shift

89 Arithmetic operations
Arithmetic operations involve adding, subtracting, multiplying, dividing, and so on. You can apply these operations to integers and floating-point numbers. The multiplication operation can be implemented in software using repeated addition or in hardware using other techniques. The division operation can also be implemented in software using repeated subtraction or in hardware using other techniques.

90 Arithmetic Operations
Table: adding bits Number of 1s Result Carry 1 2 3 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.

91 ? + + ? (+24) + (-17) = +7 (+127) + (+3) = +130 1 1 1 1 1
Example: add the following numbers in two’s complement representation: (+24) + (-17) ; (+127) + (+3) . ? (+24) + (-17) = +7 (+127) + (+3) = +130 Carry Result discard ? What’s wrong??? Data overflow !!! (-126)10

92 (-1) + 0 = -1 - (2N-1) --- 0 --- +(2N-1 –1) 127+3= -126 127+1= -128
, Overflow (-1) + 0 = -1 Overflow will happen when the value exceeds the range of the allocation. Range of numbers in two’s complement representation: - (2N-1) (2N-1 –1) 127+3= -126 127+1= -128 , ,

93 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. To subtract, negate (two’s complement) the second number and add. For example: (+101) - (+62)  (+101) + (-62) Carry Result  39 The leftmost carry is discarded.

94 Addition and subtraction for floating-point numbers
Check the signs. If different, use the sign of the number whose absolute value is larger. Move the decimal point to make the exponents the same. Add or subtract the mantissas (including the integer part and fraction part). Normalize the result before storing in memory. Check for overflow.

95 Example Add two floats: Solution The exponents are 5 and 3. The numbers are: +25 × and × Make the exponents the same. (+25 × )+ (+25× )  +25 × After normalization +26 × , which is stored as:

96 True False It’s a cat , not a tiger ! It’s a red tulip !
Logical Operations Some essential concepts It’s a cat , not a tiger ! True It’s a red tulip ! False Each logical variable can only be True or False. In the computer system, “1” means “True”, “0” means “False”.

97 Some essential concepts
According to the number of operands they take, operators can be categorized as: unary, binary and Ternary. Unary Input Output Binary Input Output

98 Some essential concepts
Logical Operations Unary NOT Binary AND OR XOR

99 Some essential concepts
A truth table shows all the possible input combinations of input with the corresponding output. x y x AND y 1 x NOT x 1 x y x OR y 1 x y x XOR y 1

100 NOT operator The NOT operator has one input. It inverts bits; that is, it changes 0 to 1 and 1 to 0.

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

102 AND operator

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

104 Inherent rule of the AND operator
If a bit in one input is 0, you can quickly conclude that the result is 0.

105 OR operator

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

107 Inherent rule of the OR operator
If a bit in one input is 1, you can quickly conclude that the result is 1.

108 XOR operator

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

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

111 Mask A mask is a special bit pattern used to modify another bit pattern. We can use masks to unset, set, or reverse specific bits.

112 Example of unsetting specific bits

113 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

114 Example 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 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?

115 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

116 Example of setting specific bits

117 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

118 Example Using the power plant example, how can you use a mask to to show that pump 6 is now turned on? Solution Use the mask Target OR Mask Result

119 Example of flipping specific bits

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

121 Shift operations If a bit pattern can be shifted to the right or to the left.

122 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. If you shift the original number to the left, you get , which is 118.

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

124 Solution (continued) Target a b c d e f g h AND Mask Result e Shift the new pattern three times to the right e000  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.

125 Objectives Differentiate between data and information.
Explain how text, images, audio and video are represented in computers. Explain decimal notation, binary notation, hexadecimal notation, and octal notation. Apply conversions from one number system to another.

126 Objectives Explain how integers are stored in computers (sign-and-magnitude, one’s complement, two’s complement). Explain how the Excess system works. Explain how to represent a floating-point number in computers. Apply bit operations such as arithmetic operations, logical operations, and shift operations.

127 That’s all for this chapter!


Download ppt "Chapter 2 Data and Number Representations"

Similar presentations


Ads by Google