IPL-2-Data Structure and Operation
IPL-2-Data Structure and Operation
IPL-2-Data Structure and Operation
PROGRAMMING TECHNOLOGY
• Lecturer: Thien Le
Contents
2.1. Interger data types
2.2. 2’s complement intergers
2.3. Operation on bits: arithmetic and logical
operation
2.4. Floating point data types
2
2.1. Interger data types
• In the binary number system, arbitrary numbers can
be represented with just the digits zero and one, the
minus sign (for negative numbers), and the period,
or radix point (for numbers with a fractional
component).
-1101.01012 = -13.312510
• Computer storage and processing: only binary digits
(0 and 1) may be used to represent numbers.
• If an n-bit sequence of binary digits an-1an-2… a1a0 is
interpreted as an unsigned integer A, its value is
3
2.1. Interger data types
4
2.1. Interger data types
2.1.2. Sign-magnitude representation
The most significant (leftmost) bit in the word as a sign
bit.
• If the sign bit is 0, the number is positive
• If the sign bit is 1, the number is negative
• General case
5
2.1. Interger data types
6
2.1. Interger data types
2.1.2 The signed integer
the signed-magnitude:
• leading 0 signifies apositive integer
• leading 1 signify a negative integer
• 6: 00110
• -6 : 10110
8
2.2. The 2’s complement integers
9
10
2.2. The 2’s complement integers
11
2.2. The 2’s complement integers
12
2.2. The 2’s complement integers
• For example, to convert -128 to two's complement
proceed as follows
– Represent 128 in binary to get 1000 0000b
– Complement the digits to get 0111 1111b
– Add 1 to get 1000 0000b which is 080H
13
2.2. The 2’s complement integers
Converting from two’s complement to decimal
• Suppose we are given a two's complement number N
• If the leading bit of N is 0, the number is non-negative and
may be converted to decimal in the usual way
• If the leading bit of N is 1, convert it to decimal as follows:
– Subtract 1 from the representation
– Complement the digits
– Convert to decimal in the usual way
– Add a - to the front of the result
• OR
– For an n-bit number, compute the decimal value of the unsigned
binary number composed of the least significant n-1 bits
– Subtract 2n-1 from the result
14
Converting from two’s complement to decimal
• Example
• Represent 0101 0110b in decimal
– This may be done in the usual way
– 64 + 16 + 4 + 2 = 86
– Represent 1010 1011b in decimal
• Since the leading bit is 1, this is a negative number which
must be converted as follows
– Subtract 1 to get 1010 1010b
– Complement the digits to get 0101 0101b
– Converting as in the previous example give 85
– Adding the minus sign gives -85
• OR
– 010 1011b = 43
– 43 - 128 = 85
15
Converting from two’s complement to decimal
• Example
16
2.3. Operation on bits: arithmetic operation
17
2.3. Operation on bits: arithmetic operation
18
2.3. Operation on bits: arithmetic operation
19
Two’s complement addition and overflow
20
Two’s complement subtraction
21
2.3. Operation on bits:
arithmetic and logical operation
2.3.1 Addition and Subtraction: Example:
• 11 + 3
The decimal value 11 is represented as 01011
The decimal value 3 is represented 00011
The sum, which is the value 14, is 01110
• 14 - 9
The decimal value 9 is represented as 01001
First we form the negative, that is, -9: 10111
The decimal value 14 is represented as 01110
we get which results in the value 5. 00101
22
2.3. Operation on bits:
arithmetic and logical operation
2.3.1 Addition and Subtraction: Example:
• 59 + 59
The decimal value 59 is represented as 0011 1011
The decimal value 59 is represented as 0011 1011
carry 0011 0011
The sum is represented as 0111 0110
• 59*2
The decimal value 59 is represented as 0011 1011
Shift left one digit 0111 0110
The decimal value 59*2 is represented as 0111 0110
23
2.3. Operation on bits:
arithmetic and logical operation
2.3.2 Sign-Extension:
8-bit number with one bit for sign, 7bits for value
• 124 + 9
The decimal value 124 is represented as 0111 1100
The decimal value 9 is represented as 0000 1001
carry 0111 1000
The result is represented as 1000 0101
sign = 1
Extension to 16 bits : 1000 0101
24
2.3. Operation on bits: logical operation
25
2.3. Operation on bits: logical operation
• Example
26
2.3. Operation on bits: logical operation
A B OR
0 0 0
0 1 1
1 0 1
1 1 1
• Example
27
2.3. Operation on bits: logical operation
A B XOR
0 0 0
0 1 1
1 0 1
1 1 0
• Example
28
2.4. Floating point data type
• Example:
– 16 bits 2’s complement data type:
– one bit: positive or negative
– 15 bits: magnitude of the value (precision)
– range: -215 to +215 -1
29
2.4. Floating point data type
The floating point data type allocates:
• The sign bit: positive or negative
• Some of the bits to the range of values (i.e., how big or
small)
• The rest of the bits are used for precision (the fraction
field)
30
2.4. Floating point data type
The floating point data type
• very important data type available in almost all ISAs.
• Allows very large and very tiny numbers to be
expressed at the expense of reducing the number of
binary digits of precision.
31
2.4. Floating point data type
The floating point data type allocates:
Example: -6*(5/8)
• The sign: negative
• The magnitude: 6*(5/8) = 110.1012
• Normalized: 110.101 = 1.10101 * 22
• Fraction: 0.10101 normalized
33
2.4. Floating point data type
The floating point data type allocates:
Example:
sign Exponent Fraction
0 0000 0000 000 0100 0000 0000 0000 0000
34
2.4. Floating point data type
The floating point data type allocates:
Example:
sign Exponent Fraction
0 0111 1011 000 0000 0000 0000 0000 0000
35
2.4. Floating point data type
The floating point data type allocates:
Example: sign Exponent Fraction
0 1000 0011 001 0100 0000 0000 0000 0000
39
2.4. ASCII data type
• ASCII stands for American Standard Code for Information Interchange.
• ASCII simplifies the interface between a keyboard manufactured by
one company, a computer made by another company, and a monitor
made by a third company.
• To display a particular character on the monitor, the computer must
transfer the ASCII code for that character to the electronics associated
with the monitor.
• Each key on the keyboard is identified by its unique ASCII code, for
example:
– the digit 3 expanded to 8 bits with a leading 0 is 00110011,
– the digit 2 is 00110010,
– the lowercase e is 01100101,
– the carriage return is 00001101
40
Exercises
1. Convert the following 2’s complementary binary numbers to
decimal
a. 1010
b. 0101 1010
c. 1111 1110
d. 0011 1001 1101 0011
2. Convert these decimal numbers to 8-bit 2's complement
binary nu`mber
a. 102
b. 64
c. 33
d. - 128
e. 127
41
Exercises
3. Add the following bit patterns. Leave your results in binary
form
a. 1011 + 0001
b. 0000 + 1010
c. 1100 + 0011
d. 0101 + 0110
e. 1111 + 0001
3. Add the following 2's complement binary numbers. Also
express the answer in decimal
a. 01 + 1011
b. 11 + 01010101
c. 0101 + 110
d. 01 + 10
42
Exercises
5. The following binary numbers are 4-bit 2's complement binary
numbers. Which of the following operations generate overflow?
Justify your answer by translating the operands and results into
decimal
a. 1100 + 0011
b. 1100 + 0100
c. 0111 + 0001
d. 1000 - 0001
e. 0111 + 1001
6. Compute the following. Write your results in binary
a. 0101 0111 AND 1101 0111
b. 101 AND 110
C. 1110 0000 AND 1011 0100
d. 0001 1111 AND 1011 0100
e. ( 0011 AND 0110 ) AND 1101
f . 0011 AND ( 0110 AND 1101 )
43
Exercises
7. Compute the following
a. 0 1 0 1 0 1 1 1 OR 1 1 0 1 0 1 1 1
b. 1 0 1 OR 1 1 0
c. 1 1 1 0 0 0 0 0 OR 1 0 1 1 0 1 0 0
d. 0 0 0 1 1 1 1 1 OR 1 0 1 1 0 1 0 0
e. ( 0 1 0 1 OR 1 1 0 0 ) OR 1 1 0 1
f . 0 1 0 1 OR ( 1 1 0 0 OR 1 1 0 1 )
6. Compute the following
a. NOT ( 1 0 1 1 ) OR N O T ( l l O O )
b. NOT ( 1 0 0 0 AND ( 1 1 0 0 OR 0 1 0 1 ) )
C. NOT (NOT ( 1 1 0 1 ) )
d. ( 0 1 1 0 OR 0 0 0 0 ) AND 1 1 1 1
44
Exercises
9. In the following exampl, what are the masks used for
Suppose we have eight machines that we want to monitor with respect to their availability.
We can keep track of them with an eight-bit BUSYNESS bit vector, where a bit is 1 if the
unit is free and 0 if the unit is busy. The bits are labeled, from right to left, from 0 to 7.
The BUSYNESS bit vector 11000010 corresponds to the situation where only units 7, 6,
and 1 are free, and therefore available for work assignment.
Suppose work is assigned to unit 7. We update our BUSYNESS bit vector by performing the
logical AND, where our two sources are the current bit vector 11000010 and the bit mask
01111111. The purpose of the bit mask is to clear bit 7 of the BUSYNESS bit vector. The
result is the bit vector 01000010.
Recall that we encountered the concept of bit mask in Example 2.7. Recall that a bit mask
enables one to interact some bits of a binary pattern while ignoring the rest. In this case,
the bit mask clears bit 7 and leaves unchanged (ignores) bits 6 through 0.
Suppose unit 5 finishes its task and becomes idle. We can update the BUSYNESS bit vector
by performing the logical OR of it with the bit mask 00100000. The result is 01100010.
45
Exercises
9. Write IEEE floating point representation of the following
decimal numbers
a. 3.75
b - 55.75
c. 3.1415927
d. 64,000
10. Write the decimal equivalents for these IEEE floating point
numbers
a. 0 10000000 00000000000000000000000
b. 1 10000011 00010000000000000000000
c. 0 11111111 00000000000000000000000
d. 1 10000000 10010000000000000000000
46
Exercises
11. Answer the questions
a. What is the largest exponent the IEEE standard allows for a 32-
bit floating point number?
b. What is the smallest exponent the IEEE standard allows for a
32-bit floating point number?
12. Translate the following ASCII codes into strings of characters
by interpreting each group of eight bits as an ASCII character
a. x48656c6c6f21
b. x68454c4c4f21
c. x436f6d70757465727321
d. x4c432d32
47
Exercises
13. Convert the following unsigned binary numbers to
hexadecimal
a. 1101 0001 1010 1111
b. 001 1111
c. 1
d. 1110 1101 1011 0010
a. x10
b. x801
c. xF731
d. X0F1E2D
e. xBCAD
48
Exercises
15. Perform the following additions. The corresponding 16-bit binary
numbers are in 2's complement notation. Provide your answers in
hexadecimal
a. x025B + x26DE
k x7D96 + xFOAO
c. xA397 + xA35D
d. x7D96 + x7412
49