Title: Lecture 2 The information layer: Data and Number Representations
1Lecture 2 The information layer Data and
Number Representations
2Outline
- Data Representation
- Data and Information
- Data Types
- Digital Data Representation
- How To Represent Different Types of Data
- Number Representation
- Number Systems
- Conversion Between Number Systems
- Integer Representation
- Floating-point Representation
- Operations on Bits
- Arithmetic Operations
- Logical Operations
3Objectives
- 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.
4Objectives
- Explain how integers are stored in computers
(sign-and-magnitude, ones complement, twos
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.
5Part 1
6Overview
- Whats data?
- Data refers to the symbols that a computer
uses to represent facts (such as people, events
and things) and ideas. - Whats 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.
7Graphics
Data
Information
Table
8Overview (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.
information
Data
9Overview (con.)
- There are several types of data.
The computer industry uses the termmultimedia
to define information that contains numbers,
text, images, audio, and video.
Note
10Question How do you handle all these data types
(text, number, image, audio and video)?
Text
1234890.
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.
12Digital Data Representation
- Digital device works with discrete, distinct data
or digits, such as 1 and 0. - Analog device works with continuous data.
- 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.
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.
1 0 0 0 1 0 1 0 1 0 1 1 1 1 1 1
- Byte is a kind of bit pattern. Its length is 8
bits.
1 byte 8 bits
15Data 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
16Text 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.
17Text Representation (con.)
18Text 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 16
19(No Transcript)
20Text 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
21Popular Code Schemes
- ASCII Code
- 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.
22Popular Code Schemes
- Features of ASCII code
- Range of 7-bit pattern is from 0000000 to
1111111. - The first pattern (000 0000) represents the null
character. - The last pattern (111 1111) represents the delete
character. - There are 31 control characters.
23(No Transcript)
24Popular 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 00000000 to 01111111. 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.
25Popular 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 65 536 characters.
- ISO Code
- It is developed by ISO (International
Organization for Standardization), which uses 4
bytes (32 bits) to handle 4 294 967 296 (232)
characters.
26Image Representation
- 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).
27Image Representation (con.)
28Image Representation (con.)
29Image 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.
30Image 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.
31Image Representation (con.)
32Image 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.
33Audio 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.
34Audio 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.
35Audio Representation (con.)
36Audio Representation (con.)
37Part 2
38- 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.
- 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)10110221015100.
40Question how do we compare these two numbers,
25.6 and 52.6, in the decimal system?
- (25.6)10 2 101 5 100 6 10-1
- (52.6)10 5 101 2 100 6 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- 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
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 A B C D E F
42Question how do we tell which number system we
are using?
In C 0101 101 0x101
(101)2 (101) 8 (101) 10 (101) 16
Binary Octal Decimal Hexadecimal
101B 101O 101D 101H
43 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
44Binary Position Value Table
Position Power Decimal Value Binary Value
216 65536 10000 0000 0000 0000
210 1024 100 0000 0000
29 512 10 0000 0000
28 256 1 0000 0000
27 128 1000 0000
26 64 100 0000
25 32 10 0000
24 16 1 0000
23 8 1000
22 4 100
21 2 10
20 1 1
45Decimal Binary Binary (16-bit system)
0 0 0000 0000 0000 0000
1 1 0000 0000 0000 0001
2 10 0000 0000 0000 0010
3 11 0000 0000 0000 0011
4 100 0000 0000 0000 0100
5 101 0000 0000 0000 0101
6 110 0000 0000 0000 0110
7 111 0000 0000 0000 0111
8 1000 0000 0000 0000 1000
9 1001 0000 0000 0000 1001
10 1010 0000 0000 0000 1010
11 1011 0000 0000 0000 1011
12 1100 0000 0000 0000 1100
13 1101 0000 0000 0000 1101
14 1110 0000 0000 0000 1110
15 1111 0000 0000 0000 1111
How to represent (16)10 in the 16-bit system??
46How 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 1is 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.)
47How 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 databasesa
petabyte represents about 1 million gigabytes!
48Octal System
- 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 Bit Pattern Oct Digit
000 0 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 Bit Pattern Hex Digit
0000 0 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
51Decimal Binary Octal Hexadecimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 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
53Binary 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
0 1 0 1 1 0 1
Binary Number
6 5 4 3 2 1 0
Position
26
24
22
25
23
21
20
Position Value
026 125 024 123 122 021 120
0 32 0 8 4 0 1 ( 45 )10
Result
54Decimal 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.
55Example Convert the decimal number 35 to binary.
Stop when the quotient is 0
Quotient
Decimal Number
17
8
2
1
4
0
35
1
0
0
1
1
0
Remainder
Binary Number 100011
56Questions
- 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?
57Binary 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 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0
1
7
6
3
4
4
If the leftmost bit pattern does not contain 3
digits, add extra 0s to the left.
58Octal to Binary Conversion
- Solution
- Transform each octal digit into a 3-bit binary
pattern.
Octal Number
Octal Number
1 4 2
5 6 2
001
100
010
101
110
010
Binary Number
Binary Number
59Binary 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.
1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 0
F
C
E
4
If the leftmost bit pattern does not contain 3
digits, add extra 0s to the left.
60Hexadecimal to Binary
- Solution
- Transform each hexadecimal digit into a 4-bit
binary pattern.
Hexadecimal Number
2 4 C
0010
0100
1100
Binary Number
61Exercises
- (10 111 011.110 1)2 (?)8
- (273.64)8
- (6754.32)8 (?)2
- (110 111 101 100. 011 010)2
- (1011 1110 0110. 1101 1)2 (?)16
- (BE6.D8)16
- (A7B8.C9)16 (?)2
- (1010 0111 1011 1000. 1100 1001)2
62- Integers are the whole numbers, which include
positive integers and negative integers. - The figure below shows different integers.
Integers
Unsigned
Signed
Sign-and- Magnitude
Ones Complement
Twos 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.
64Unsigned 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
0000 0111
0000 0000 0000 0111
0000 0000 1110 1010
1110 1010
overflow
0000 0001 0000 0010
0110 0000 1011 1000
overflow
overflow
overflow
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 ? 00000000 -0 ? 10000000
- 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 its positive, add 0 if negative,
add 1.
67Example 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
0
0
0
0
0
0
0
0
0
1
1
1
-258
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
68 Signed Integers
- Ones 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 ones 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 ones complement integer.
69 Note
- There are two 0s in ones complement
representation, too positive zero (0) and
negative zero (-0). - ---For example In an 8-bit allocation,
- 0 ? 00000000 -0 ? 11111111
- How to store a ones 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.
70Example Store 7 and -7 in an 8-bit memory
location, and store 258 in a 16-bit memory
location using ones complement representation .
7
-7
1
1
1
1
1
1
1
1
1
0
0
0
0
0
1
1
1
0
0
0
0
0
1
1
1
1
0
0
0
1
1
0
0
0
0
0
0
1
0
-258
1
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
71 Note
- Ones complementing means reversing all bits.
- If you ones complement a positive number, you
get the corresponding negative number. - If you ones complement a negative number, you
get the corresponding positive number. - If you ones complement a number twice, you get
the original number.
72 Signed Integers
- Range of signed integers -(2N-1) ( 2N-1-1)
- --- N is the number of bits allocated to
represent a twos complement integer. - How to store a twos 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.
Twos complement is the most common, the most
important, and the most widely used
representation of integers today.
73- For twos complement one can also use the
following formula to compute the negative
representation - Negative(I)2K-I, where k is the number of
bits -
- Example Assume four bits are used for twos
complement representation, for the number 0010,
we can derive its negative representation as
follows - Negative(0010) 24-00101110
-
74Example Store 7 in an 8-bit memory location,
and store 40 in a 16-bit memory location using
twos complement representation .
7
1
1
1
1
1
1
0
0
0
0
0
1
0
1
0
0
0
-40
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
1
1
1
1
1
1
1
1
1
0
1
1
0
0
0
1
Note
For negative numbers Twos complement ones
complement 1
75 Note
- In twos 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 twos complement
- For example in an 8-bit allocation,
- 0 ? 00000000
- If you twos complement a positive number, you
get the corresponding negative number. - If you twos complement a negative number, you
get the corresponding positive number. - If you twos complement a number twice, you get
the original number.
76Summary of integer representation
Unsigned ------------ 0 1 2 3 4 5 6 7 8 9 10 11 1
2 13 14 15
Sign-and-Magnitude --------- 0 1 2 3 4 5 6
7 -0 -1 -2 -3 -4 -5 -6 -7
OnesComplement --------- 0 1 2 3 4 5 6 7
-7 -6 -5 -4 -3 -2 -1 -0
TwosComplement -------- 0 1 2 3 4 5 6 7
-8 -7 -6 -5 -4 -3 -2 -1
Contents of Memory------------ 0000 0001 0010 001
1 0100 0101 0110 0111 1000 1001 1010 1011 1100 110
1 1110 1111
77 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.
78 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
1100110. - Step 3 Add one 0 to make a total of 8 bits. The
representation is 01100110
79- Floating-point Representation
Fraction Part
1 4 . 2 3 4
Integer Part
1 4
2 3 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
80Changing fractions to binary
Multiply 2 to gain integer part
Multiply by 2 to get integer part
Stop when the result is 0
0.250
0.500
1.000
0.000
0.125
0 .
0
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 .
0
1
1
0
0
1
81Example transform the number 49.58 to a binary (
3 bits after the decimal point)?
( 49 . 58 )10 ( . )2
110001
100
Integer Part
Fraction Part
82- 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
261.01000111001
1010001 . 11001
3 digits
- 2-31.110011
- 0 . 001110011
2e 1.XXXXXXX
Sign (1 bit)
Mantissa (unsigned integer)
Exponent (Excess representation)
83- Floating-point Representation
Single-precision format 4 bytes 32 bits
sign
1
8
23
exponent
mantissa
1
10
52
Double-precision format 8 bytes 64 bits
84- Floating-point Representation
85 Example Show the representation of the
normalized number 26 1.01000111001
- 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 0 10000101
01000111001000000000000
86 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
-2-3 1.110011
87Quiz
- The decimal equivalent of the binary number
11.11 is . - Show the following number in 32-bit IEEE format
. - -2-51.01101001
- Suppose the following bit pattern represents a
value stored in twos complement notation. Find
the twos complement representation of the
negative of the value. - 01010101
88Part 3
89Bit Operations
Arithmetic
Logical
Shift
90Arithmetic 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.
91Arithmetic Operations
Table adding bits
Number of 1s Result Carry
0 0
1 1
2 0 1
3 1 1
- Rule of Adding Integers in Twos 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.
92Example add the following numbers in twos
complement representation(24) (-17) (127)
(3) .
?
(24) (-17)
(127) (3)
7
130
1 1 1 1 1
1 1 1 1 1 1 1
Carry
0 0 0 1 1 0 0 0
0 1 1 1 1 1 1 1
1 1 1 0 1 1 1 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 1 1
1 0 0 0 0 0 1 0
Result
discard
?
(-126)10
Whats wrong??? Data overflow !!!
93Overflow
1 1 1 1 , 1 1 1 1
(-1) 0 -1
- Overflow will happen when the value exceeds the
range of the allocation. - Range of numbers in twos complementrepresentatio
n - - (2N-1) --- 0 --- (2N-1 1)
1273 -126
1271 -128
1 0 0 0 , 0 0 1 0
1 0 0 0 , 0 0 0 0
94 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 (twos complement) the second
number and add. For example - (101) - (62) ?? (101) (-62)
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 ?
39The leftmost carry is discarded.
95Addition 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.
96Example
Add two floats0 10000100 1011000000000000000000
00 10000010 01100000000000000000000
Solution
The exponents are 5 and 3. The numbers are25
1.1011 and 23 1.011Make the exponents
the same.(25 1.1011) (25 0.01011) ? 25
10.00001After normalization 26 1.000001,
which is stored as0 10000101
000001000000000000000000
97 Logical Operations
Its a cat , not a tiger !
True
Its a red tulip !
False
- Each logical variable can only be True or False.
- In the computer system, 1 means True, 0
means False.
98 Some essential concepts
- According to the number of operands they take,
operators can be categorized as unary, binary
and Ternary.
99 Some essential concepts
100 Some essential concepts
- A truth table shows all the possible input
combinations of input with the corresponding
output.
x y x AND y
0 0 0
0 1 0
1 0 0
1 1 1
x NOT x
0 1
1 0
x y x OR y
0 0 0
0 1 1
1 0 1
1 1 1
x y x XOR y
0 0 0
0 1 1
1 0 1
1 1 0
101NOT operator
The NOT operator has one input. It inverts bits
that is, it changes 0 to 1 and 1 to 0.
102Example
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
103AND operator
104Example
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
105Inherent rule of the AND operator
If a bit in one input is 0, you can quickly
conclude that the result is 0.
106OR operator
107Example
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
108Inherent rule of the OR operator
If a bit in one input is 1, you can quickly
conclude that the result is 1.
109XOR operator
110Example
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
111Inherent 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.
112Mask
A mask is a special bit pattern used to modify
another bit pattern. We can use masks to unset,
set, or reverse specific bits.
113Example of unsetting specific bits
114Example
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
ANDMask 0 0 0 0 0 1 1 1
------------------Result
0 0 0 0 0 1 1 0
115Example
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?
116Solution
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 ANDMask 1 0
1 1 1 1 1 1
------------------Result 1 0 0 0
0 1 1 1
117Example of setting specific bits
118Example
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
ORMask 1 1 1 1 1 0 0 0
------------------Result
1 1 1 1 1 1 1 0
119Example
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 00100000. Target 1 0 0 0 0 1 1 1
ORMask 0 0 1 0 0 0 0 0
------------------Result
1 0 1 0 0 1 1 1
120Example of flipping specific bits
121Example
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
122Shift operations
If a bit pattern can be shifted to the right or
to the left.
123Example
Show how you can divide or multiply a number by2
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.
124Example
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.
125Solution (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 ? 0000000eNow 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.
126Objectives
- 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.
127Objectives
- Explain how integers are stored in computers
(sign-and-magnitude, ones complement, twos
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.
128Thats all for this lecture!