IPL-2-Data Structure and Operation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

SAIGON UNIVERSITY

FACULTY OF ELECTRONICS AND TELECOMMUNICATIONS

PROGRAMMING TECHNOLOGY

2 - Data Structure and


Operation

• 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

2.1.1 The unsigned integer

• Represent unsigned integers as strings of binary digits


• With k bits, we can represent in this positional notation
exactly 2k integers, ranging from 0 to 2k — 1.
• In our 5-bit example, we can represent the integers
from 0 to 31.

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

2.1.2 The signed integer

• Represent unsigned integers as strings of binary digits


• With k bits, we can represent in this positional notation
exactly 2k integers:
– half for positive numbers,
– half for negative number
– ranging from 0 to 2k — 1 — 1
• In our 5-bit example, we can represent the integers
from (-15 to -1) and (0 to 15).

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

The 1’s complement:


• a negative number be represented by taking the
representation of the positive number having the same
magnitude, and "flipping" all the bits.
• 6: 00110
• -6 : 11001
7
2.2. The 2’s complement integers

• To represent a negative number, follow the


following steps:
– Convert its negation to an 8-bit binary number in
the usual manner
– Take the representation computed in the previous
step and complement every digit: this means
replaces 0's by 1's and 1's by 0's
– Add 1 to the result obtained in the previous step

8
2.2. The 2’s complement integers

9
10
2.2. The 2’s complement integers

• Invert each bit in unsigned representation to obtain


one's complement
• Then add 1 to the result
• Example : - 57

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

• For example, to convert -127 to two's complement


proceed as follows
– Represent 127 in binary to get 0111 1111b
– Complement the digits to get 1000 0000b
– Add 1 to get 1000 0001b which is 081H

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

Negation of an integer number: twos complement operation


1. Take the Boolean complement of each bit of the integer
(including the sign bit). That is, set each 1 to 0 and each 0 to 1.
2. Treating the result as an unsigned binary integer, add 1.

17
2.3. Operation on bits: arithmetic operation

Negation of an integer number: twos complement operation

18
2.3. Operation on bits: arithmetic operation

Two’s complement overflow

• Consider these simple rules for determining if there


has been an overflow in two's complement
arithmetic
• Overflow cannot occur when adding two numbers
with different signs
• If the two numbers have the same sign, overflow
occurs when there is a "sign change" in the result

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

• Perform on binary patterns is the set of logical


operations.
• Logical operations operate on logical variables. A logical
variable can have one of two values, 0 or 1.
• The name logical:
• two values 0 and 1 can represent the two logical values
false and true

25
2.3. Operation on bits: logical operation

• 2.3.1. The AND function


A B AND
0 0 0
0 1 0
1 0 0
1 1 1

• Example

26
2.3. Operation on bits: logical operation

• 2.3.1. The OR function

A B OR
0 0 0
0 1 1
1 0 1
1 1 1

• Example

27
2.3. Operation on bits: logical operation

• 2.3.1. The XOR (Exclusive-OR) function

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

– The exponent: 22 = 2(129 - 127)


– The exponent: 129 = 10000001
• Floating number representation
sign Exponent Fraction
1 1000 0001 101 0100 0000 0000 0000 0000
32
2.4. Floating point data type
The floating point data type allocates:
sign Exponent Fraction
1/0 0000 0000 001 0100 0000 0000 0000 0000
• The sign: negative
• Exponent: 0000 0000
– The [actual] exponent: -126
• Number: - 1 * 0.fraction * 2-126

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

• The sign: positive


• Exponent: 0000 0000
– The [actual] exponent: -126
• The magnitude 23bits: = .000 0100 0000 0000 0000 0000 = 2-5
• Number:
+1.000 0100 0000 0000 0000 0000*2-126 = +1*2-5*2-126 = 2-131

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

• The sign: positive


• Exponent: 0111 1011
– The [normalized] exponent: 2123 = 2-(4 - 127)
– The [actual] exponent: 123 - 127 = -4
• The magnitude: 23bits of 0 = .00002
• Number: +1.000 0000 0000 0000 0000 0000*2-4 = +1*2-4 = 1/16

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

• The sign: positive


• Exponent: 1000 0011
– The [normalized] exponent: 2131 = 2(4 - 127)
– The [actual] exponent: 131 - 127 = +4
• The magnitude:
• 1.001 0100 0000 0000 0000 0000*24 => shift right 4 position
• 10010.100 0000 0000 0000 0000*20
• Number: +10010.1 = +18.5
36
2.4. Floating point data type
The floating point data type allocates:
Example: sign Exponent Fraction
1 1000 0010 001 0100 0000 0000 0000 0000

• The sign: negative


• Exponent: 1000 0010
– The [normalized] exponent: 2130 = 2-(3 - 127)
– The [actual] exponent: 130 - 127 = +3
• The magnitude:
• 1.001 0100 0000 0000 0000 0000*23 => shift right 3 position
• 1001.0100 0000 0000 0000 0000*20
• Number: - 1001.01 = -9.25
37
2.4. Floating point data type
The floating point data type allocates:
Example: sign Exponent Fraction
0 1111 1110 111 1111 1111 1111 1111 1111

• The sign: positive


• Exponent: 1111 1110
– The [normalized] exponent: 2254 = 2-(-127 - 127)
– The [actual] exponent: 254 - 127 = +127
• The magnitude:
• 1.111 1111 1111 1111 1111 1111*2127 => shift right 127
position
• Number: approximately 2128
38
2.4. Floating point data type
The floating point data type allocates:
Example: sign Exponent Fraction
1 0000 0000 000 0000 0000 0000 0000 0001

• The sign: negative


• Exponent: 0000 0000
– The [actual] exponent: -126
• The magnitude: 0.000 0000 0000 0000 0000 0001=2-23
• Number: -2-23 *2-126 = - 2-149

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

14. Convert the following hexadecimal numbers to binary.

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

16.Perform the following logical operations. Express your answers in


hexadecimal notation.

a. x5478 AND xFDEA


b. xABCD OR xl234
c. NOT((NOT(xDEFA)) AND (NOT(xFFFF)))
d. xOOFF XOR x325C

49

You might also like