Uploaded by Radwan Ahmed

Module 2 Data Representation and Boolean Logic Clean .pdf (1)

advertisement
MODULE-2
DATA REPRESENTATION &
BOOLEAN LOGIC
COMP 2131: INTRODUCTION TO
COMPUTER SYSTEMS
THOMPSON RIVERS UNIVERSITY
SECTION 2.1
DATA REPRESENTATION
2
Binary Number System
•
•
•
•
•
Base = 2
• 2 digits { 0, 1 }, called binary digits or “bits”
4
2
1
1/2 1/4
Weights
• Weight = (Base) Position
1 0 1
0 1
Magnitude
2
1
0
-1 -2
• Sum of “Bit x Weight”
2
1
0
-1
1
*2
+0
*2
+1
*2
+0
*2
+1
*2
2
Formal Notation
Groups of bits
4 bits = Nibble
=(5.25)10
8 bits = Byte
(101.01)2
1011
3
11000101
Octal Number System
•
•
•
•
Base = 8
• 8 digits { 0, 1, 2, 3, 4, 5, 6, 7 } 64 8 1
1/8 1/64
Weights
5 1 2
7 4
• Weight = (Base) Position
2
1
0
-1 -2
Magnitude
2
1
0
-1
5
*8
+1
*8
+2
*8
+7
*8
+4
*8
• Sum of “Digit x Weight”
2
Formal Notation
=(330.9375)10
(512.74)8
Hexadecimal Number System
• Base = 16
• 16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F }
1/16 1/256
• Weights
256 16
1
• Weight = (Base) Position
1 E 5
7 A
• Magnitude
2
1
0
-1 -2
• Sum of “Digit x Weight” 2
1
0
-1
-2
1
*16
+14
*16
+5
*16
+7
*16
+10
*16
• Formal Notation
=(485.4765625)10
(1E5.7A)16
The Power of 2
n
2n
n
2n
0
20=1
8
28=256
1
21=2
9
29=512
2
22=4
10
210=1024
3
23=8
11
211=2048
4
24=16
12
212=4096
5
25=32
20
220=1M
Mega
6
26=64
30
230=1G
Giga
7
27=128
40
240=1T
Tera
Kilo
Conversion Chart
Uses two digits 0 and 1.
02
12
102
112
1002
1012
1102
1112
10002
10012
...
7
=
=
=
=
=
=
=
=
=
=
0×20
1×20
1×21
1×21
1×22
1×22
1×22
1×22
1×23
1×23
=
=
+
+
+
+
+
+
+
+
0×20
1×20
0×21
0×21
1×21
1×21
0×22
0×22
=
=
+
+
+
+
+
+
0×20
1×20
0×20
1×20
0×21
0×21
=
=
=
=
+ 0×20 =
+ 1×20 =
010
110
210
310
410
510
610
710
810
910
Number Base
Conversions
Evaluate
Magnitude
Octal
(Base 8)
Evaluate
Magnitude
Decimal
(Base 10)
Binary
(Base 2)
Hexadecimal
(Base 16)
Evaluate Magnitude
Decimal to Binary Conversion
• Divide the number by the ‘Base’ (=2)
• Take the remainder (either 0 or 1) as a coefficient
• Take the quotient and repeat the division
Example: (13)10
Quotient
13
6
3
1
/2=
/2=
/2=
/2=
Answer:
6
3
1
0
Remainder
1
0
1
1
Coefficient
a0 = 1
a1 = 0
a2 = 1
a3 = 1
(13)10 = (a3 a2 a1 a0)2 = (1101)2
MSB
LSB
Decimal to Binary Conversion
(Fractions)
• Multiply the number by the ‘Base’ (=2)
• Take the integer (either 0 or 1) as a coefficient
• Take the resultant fraction and repeat the division
Example: (0.625)10
Integer
0.625 * 2 =
0.25 * 2 =
0.5
*2=
Answer:
1
0
1
.
.
.
Fraction
Coefficient
25
5
0
a-1 = 1
a-2 = 0
a-3 = 1
(0.625)10 = (0.a-1 a-2 a-3)2 = (0.101)2
MSB
LSB
Decimal to Octal Conversion
Example: (175)10
Quotient
175 / 8 =
21 / 8 =
2 /8=
Remainder
21
2
0
Answer:
7
5
2
Coefficient
a0 = 7
a1 = 5
a2 = 2
(175)10 = (a2 a1 a0)8 = (257)8
Example: (0.3125)10
Integer
0.3125 * 8 =
0.5
*8=
Answer:
2
4
.
.
Fraction
Coefficient
5
0
a-1 = 2
a-2 = 4
(0.3125)10 = (0.a-1 a-2 a-3)8 = (0.24)8
Binary − Octal Conversion
• 8 = 23
• Each group of 3 bits represents an
octal digit
Assume Zeros
Example:
( 1 0 1 1 0 . 0 1 )2
( 2
6
. 2 )8
Works both ways (Binary to Octal & Octal to
Binary)
Octal Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111
Binary − Hexadecimal
Conversion
• 16 = 24
• Each group of 4 bits represents a
hexadecimal digit
Assume Zeros
Example:
( 1 0 1 1 0 . 0 1 )2
(1
6
. 4 )16
Hex
Binary
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Works both ways (Binary to Hex & Hex to Binary)
Octal − Hexadecimal Conversion
Example:
• Convert to Binary as an intermediate step
( 2
6
.
2 )8
Assume Zeros
Assume Zeros
( 0 1 0 1 1 0 . 0 1 0 )2
(1
6
.
4 )16
Works both ways (Octal to Hex & Hex to Octal)
Decimal, Binary, Octal and Hexadecimal
Decimal
Binary
Octal
Hex
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
00
01
02
03
04
05
06
07
10
11
12
13
14
15
16
17
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Hexadecimal Number System
010 = 00002 = 016 = 0x0
110 = 00012 = 116 = 0x1
210 = 00102 = 216 = 0x2
310 = 00112 = 316 = 0x3
410 = 01002 = 416 = 0x4
510 = 01012 = 516 = 0x5
610 = 01102 = 616 = 0x6
710 = 01112 = 716 = 0x7
810 = 10002 = 816 = 0x8
910 = 10012 = 916 = 0x9
1010 = 10102 = A16 = 0xA
1110 = 10112 = B16 = 0xB
1210 = 11002 = C16 = 0xC
1310 = 11012 = D16 = 0xD
1410 = 11102 = E16 = 0xE
1510 = 11112 = F16 = 0xF
16
4 bits can be used for a
hexadecimal number, 0, ..., F.
Please memorize it!
Exercise
•
Complete practice problems 2.1, 2.2, 2.3, 2.4 from
Bryant and O’Hallaron.
Data representation in Words
•
•
18
A word size – It is the number of bits processed by
a computer's CPU in one go i.e. typically 32 bits or
64 bits
Data bus size, instruction size, address size are
usually multiples of the word size.
Addressing and Byte Ordering
A variable x of type int (allocates 4 bytes)
• If the address of x: 0x100 (means it starts storing from 0x100)
• This means the 4 bytes of x would be stored in memory locations 0x100,
•
•
0x101, 0x102, and 0x103.
Let’s assume x has the value 0x1234567, which needs to be stored in four
bytes.
There are two conventions to store the values in the 4 consecutive byte
memory locations. 0x01, 0x23, 0x45, and 0x67, or 0x67, 0x45, 0x23, and 0x01,
depending on CPU architecture.
Little endian byte order – Intel-compatible machines
0x103
0x01
0x102
0x23
0x101
0x45
0x100
0x67
address
value
Big endian byte order – machines from IBM and Sun Microsystems
0x103
0x67
19
0x102
0x45
0x101
0x23
0x100
0x01
Integer Representations in size
20
C data type
Java Data type
(Equivalent)
Size
Char, unsigned
char
byte
1B
Short, unsigned
short
Char or short
2B
Int, unsigned int
int
4B
Long, unsigned
long
long
8B
float
float
4B
Double
double
8B
Unsigned Encodings – sign of
the digit is not stored
unsigned char
All 8 bits are used for data storage
• No sign bit.
• The smallest number is 0
• The maximum number is 0xff.
unsigned short 16 bits
• The smallest number is 0
• The maximum number is 0xffff – 65536 in decimal
unsigned int
32 bits
• The smallest number is 0
• The maximum number is 0xffffffff
unsigned long
64 bits
• The smallest number is 0
• The maximum number is ?
21
Representation of Unsigned
Integers
•
8-bit representation of unsigned char
255
254
...
128
127
126
...
2
1
0
11111111
11111110
...
10000000
01111111
01111110
...
00000010
00000001
00000000
+1
+1
+1
overflow
•
•
•
22
The maximum number is 28 -> 255
The minimum number is 0
if we add the maximum number by 1, it becomes 256 i.e. 1 0000 0000 and can only
store 0
SECTION 2.1 CONT.
BINARY ARITHMETIC
23
Binary Addition
•
We will discuss binary addition and binary subtraction, before we
discuss the representation of signed integers.
•
•
How to add two binary numbers? Let’s consider only unsigned integers
(i.e., positive numbers only) for a while.
Just like the addition of two decimal numbers.
•
E.g.,
+
10010
1001
11011
carry
10010
+
1011
11101
10111
+
111
???
24
1111
+
1
10000
Decimal Addition
• Decimal Addition
1
+
1
1
Carry
5
5
5
5
1
0
= Ten ≥ Base
 Subtract a
Base
Binary Addition
• Column Addition
1 1 1 1 1 1
1 1 1 1 0 1
= 61
1 0 1 1 1
= 23
1 0 1 0 1 0 0
= 84
+
≥ (2)10
Binary Subtraction
• Borrow a “Base” when needed
1
2
0 2 2 0 0 2
−
= (10)2
1 0 0 1 1 0 1
= 77
1 0 1 1 1
= 23
0 1 1 0 1 1 0
= 54
Binary Multiplication
•
Bit by bit
1 0 1 1 1
x
1 0 1 0
0 0 0 0 0
1 0 1 1 1
0 0 0 0 0
1 0 1 1 1
1 1 1 0 0 1 1 0
Subtraction as Addition
• We know, 10010 – 11 = 1111, but there is no
subtraction.
• So, we add
00010010
+ 11111100
1 00001110
+
1
00001111
• There is a relationship between 112 and 111002
• The 1’s complement of 000112 is 11100
• This type of addition is called 1’s complement
addition.
29
2’s Compliment
• Now, we add
00010010
+ 11111101
1 00001111
• 11101 is 2’s compliment of 00011?
• 2’s complement ≡ 1’s complement + 1 -> 11100 +
1 = 11101
• This type of addition is called 2’s complement
addition.
30
Example
•
Another example
-
•
What if we use 1’s complement addition or 2’s
complement addition instead as follow? Let’s use
8-bit representation.
+
1
+
•
101010
101
???
00101010
11111010
00100100
1
00100101
00101010
11111011
00100101
What does this mean?
• A – B = A + (–B), where A and B are positive
• Is the 1’s complement or the 2’s complement of B sort of
equal to –B?
31
+
1
Representation of Negative
Binaries in Memory
•
•
•
•
•
32
Representation of signed integers
8 or 16 or 32 bits are usually used for integers.
Let’s use 8 bits for examples.
• The left most bit (called most significant bit) is used as sign.
• When the MSB is 0, non-negative integers.
• When the MSB is 1, negative integers.
• The other 7 bits are used for integers.
To represent positive integer 9:
• 00001001
How about -9:
• 10001001 is really okay?
• 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong!
• We need a different representation for negative integers.
Overflow of signed numbers
•
8-bit representation of
signed char with 2’s complement
•
•
•
•
127
01111111
126
01111110
...
...
overflow
overflow
2
00000010
1
00000001
0
00000000
+1
-1
-1
11111111
-2
11111110
-3
11111101
+1
-1
...
...
+1
-1
-127
10000001
-128
10000000
The maximum number is 127
The minimum number is -128
What if we add 1 to the maximum i.e. -128 -> it reaches back to 127
What if we subtract 1 from the minimum number i.e. 127 -> it reaches
back to -128
Ref: for further details read section 2.3 Integer Arithmetic from the textbook.
33
Signed representations
•
•
•
34
Note that computers use the 8-bit representation, the 16-bit
representation, the 32-bit representation and the 64-bit
representation with 2’complement for negative integers.
In programming languages
• char,
unsigned char
8-bits
• short,
unsigned short
16-bits
• int,
unsigned int
32-bits
• long,
unsigned long
64-bits
When we use the 32-bit representation with 2’s complement,
• The maximum number is ?
• What if we add the maximum number by 1 ???
• The minimum number is ?
• What if we subtract the minimum number by 1 ???
•
•
•
Now we know how to represent negative integers.
2’ complement addtion A + (–B) is computed for subtraction A –
B.
Let’s suppose B is negative. Then –B is really a positive integer?
For example, let’s consider 1 byte signed integer.
127
126
...
2
1
0
-1
-2
-3
...
-127
-128
•
35
01111111
01111110
...
00000010
00000001
00000000
11111111
11111110
11111101
...
10000001
10000000
2’s complement of -3 is 00000011, i.e., 3.
2’s complement of -128 is 10000000 again.
For any -127 < x < 127, x – x = 0. But (-128) – (-128) = ???
2’s compliment representation
• We need a different representation for negative
integers.
• What is the 8-bit 1’s complement of 9?
• 11110110
<- 8-bit 1’s
complement of 9
• 00001001 + 11110110 <- 9 + 8-bit 1’s complement
of 9
= 11111111
<- Is it zero? (1’s complement
of 0?)
• What is the 2’s complement of 9?
• 11110111
<- 8-bit 2’s complement of 9
• 00001001 + 11110111 <- 9 + 8-bit 2’s complement
of 9
= 1 00000000
<- It looks like zero.
• 2’s complement representation is used for negative
integers.
36
Binary Multiplication

Bit by bit
1 0 1 1 1
x
1 0 1 0
0 0 0 0 0
1 0 1 1 1
0 0 0 0 0
1 0 1 1 1
1 1 1 0 0 1 1 0
37
Fractional number
Representation
•
•
Fractional binary numbers
IEEE floating-point representation
•
A floating-point number is typically expressed
in the scientific notation, with a fraction (F), and
an exponent (E) of a certain radix (r), in the
form of F×r^E.
Decimal numbers use radix of 10 (F×10^E);
while binary numbers use radix of 2 (F×2^E).
•
38
Fractions: Fixed-Point
• Use “binary point” to separate positive from
negative powers of two -- like “decimal point.”
• 2’s comp addition and subtraction still work.
(Assuming binary points are aligned)
2-1 = 0.5
2-2 = 0.25
2-3 = 0.125
00101000.101 (40.625)
+ 11111110.110 (-1.25) (2’s complement)
00100111.011 (39.375)
39
No new operations -- same as integer arithmetic.
Floating-Point number storage
•
•
Need to represent F (fraction), E (exponent),
and sign.
Floats are stored in memory as follows: they
are decomposed into:
• sign s (denoting whether it's positive or negative) - 1
•
•
bit
mantissa m (essentially the digits of your number - 23
bits
exponent e - 8 bits
•
IEEE 754 Floating-Point Standard (32-bits):
1b
8 bits
S Exponent
40
23 bits
Fraction (mantissa)
More information is available on the Floating Point Guide
Three different cases
• Case 1 : Normalized Values
• Case 2 : Denormalized values
• Case 3 : Special values
Normalized Value
•
The bit pattern of exp is neither all zero nor all one.
In this case, the exponent field is interpreted as
representing a signed integer in biased form. The
exponent value is E= e – Bias, where e is the
unsigned number having bit representation and
bias as 127 for single precision and 1023 for
double.
De-normalized numbers
• Ii is the case where the exponent is all 0s, but the fraction is
non-zero. This will have an assumed leading 0 before the
binary point. Thus, this represents a number (−1)s × 0.f × 2−126,
where s is the sign bit and f is the fraction.
• For double precision, denormalized numbers, the form
changes to (−1)s × 0.f × 2−1022
• The denormalized numbers gradually lose their precision as
they get smaller because the left bits of the fraction become
zeros.
• The smallest denormalized value (only the least-significant
fraction bit is one), a 32-bit floating-point number always
have single bit of precision as compared to the standard
24-bits for normalized values.
Special case
• A final category of values occurs when the exponent field is all
ones. When the fraction field is all zeros, the resulting values
represent infinity, either +∞ when s = 0, or −∞ when s = 1.
Infinity can represent results that overflow, as when we
multiply two very large numbers, or when we divide by zero.
When the fraction field is nonzero, the resulting value is called
a “NaN,” short for “Not a Number.”
• Such values are returned as the result of an operation where
the result cannot be given as a real number or as infinity, as
when computing √−1 or ∞−∞. They can also be useful in some
applications for representing uninitialized data.
Example
•
For memory representation of 5.2 in memory:
• First convert it into simple binary system, which will give us
101.001100110011001100110011
• Now change it into scientific form :
1.01001100110011001100110011 x 10^2 .
• Now our sign bit is 0 as the number is positive
• The exponent does not have a sign; instead an exponent
bias is subtracted from it (127 for single and 1023 for
double precision). This, and the bit sequence, allows
floating-point numbers to be compared and sorted correctly
even when interpreting them as integers. (so, 2-127 = 125=10000001)
• Fraction is 01001100110011001100110 . (23 bits)
=> the final representation is
• 0 10000001 0100 1100 1100 1100 1100 110
45
ASCII Character Codes
•
•
•
•
•
American Standard Code for Information
Interchange (Refer to Table 1.7)
A popular code used to represent information sent
as character-based data.
It uses 7-bits to represent:
• 94 Graphic printing characters.
• 34 Non-printing characters.
Some non-printing characters are used for text
format (e.g. BS = Backspace, CR = carriage return).
Other non-printing characters are used for record
marking and flow control (e.g. STX and ETX start
and end text areas).
ASCII Properties
• ASCII has some interesting properties:
• Digits 0 to 9 span Hexadecimal values 3016 to
•
•
3916
Upper case A-Z span 4116 to 5A16
Lower case a-z span 6116 to 7A16
• Lower to upper case translation (and vice versa) occurs
by flipping bit 6.
SECTION 2.2
BOOLEAN LOGIC
48
Unit Learning Objectives
Upon completion of this section, students will
be able to:
• Perform Boolean logic
• Understand Logic gates, Binary
Expressions and truth tables
• Draw simple Combinational Logic diagram
49
Boolean Logic
•
•
Named on George Boole, this is a branch of
mathematics that defines the rules for manipulating
the two logical truth values true and false
Boolean logic is very important for computers, as:
• It is a way to directly map binary digits
• 1 = true, 0 = false
50
Boolean Expression
•
•
A Boolean expression is any expression that
evaluates to either true or false.
Is the expression 1+3 a Boolean expressions?
• No, doesn’t evaluate to either true or false.
•
Examples of Boolean expressions:
•
•
•
•
51
X > 100
X<Y
A = 100
2>3
Introduction
Boolean expressions
• The three following operators to construct more
complex Boolean expressions
• AND
• OR
• NOT
52
Truth Table for AND
•
Let a and b be any Boolean expressions, then
a
False
False
True
True
Examples
53
b
False
True
False
True
a AND b
False
False
False
True
X is 1 and Y is 5
X>0 AND X<20
True
X=10 AND X>Y
False
Truth Table for OR
• Let a and b be any Boolean expressions, then
a
False
False
True
True
Examples
54
b
False
True
False
True
X is 1 and Y is 5
X>0 OR X<20
True
X=10 OR X>Y
True
a OR b
False
True
True
True
Truth Table for NOT
•
Let a be any Boolean expression, then
Examples
NOT X>0
NOT X>Y
55
a
NOT a
False
True
True
False
X is 1 and Y is 5
False
True
Gates
• Gates operates on a collection of
binary inputs to produce a binary
output.
• Three main gates that implement
the Boolean operators are:
• AND (.)
• OR (+)
• NOT (!)
5
6
Binary Logic
AND
OR
x
y
z
x
y
z
x
z
0
0
0
0
0
0
0
1
0
1
0
0
1
1
1
0
1
0
0
1
0
1
1
1
1
1
1
1
z=x•y=xy
x
y
57
NOT
z
z = x’
z=x+y
x
y
z
x
z
More gates
•
•
58
NAND = NOT+ AND = (a.b)’
NOR = NOT + OR = (a+b)’
Boolean Function
•
A Boolean function is made up of:
At least one Boolean variable, (A, b, c …)
• At least one Boolean operator, (., + or !)
• At least one input from the set {0,1}.
•
59
•
It produces the output also in the form of {0,1}.
•
Ex: (three variables, three operators and
Boolean result
Boolean Algebra
• To make evaluation of the Boolean function
given on the last slide, the following truth
table is showing the evaluations as per the
precedence of the operators.
• precedence is:
• NOT
• AND
• OR
60
Logic Gates
• Every Boolean Expression can be depicted
using the gate operations
• F(x, y, z) = x+y+z
• F(x,y,z)=x.y’.z
61
Combinational Circuits
•
Combinational Logic Circuits are made up from basic logic
NAND, NOR, NOT gates that are connected together to
produce further switching circuits.
• There are three main ways of specifying the function of a
combinational logic circuit :
1. Boolean Algebra – algebraic expression showing the
operation of the logic circuit for each input variable either
True or False that results in a logic “1” output.
2. Truth Tables – A table defines the function of a logic gate by
providing all the inputs and output states in tabular form for
each possible combination of input as in expression.
3. Logic Diagram – The graphical representation of a logic
circuit showing wiring and connections of each individual
logic gate,
62
Combinational Circuits
Let us see a Boolean function:
The circuit for this expression is
This is Combinational logic circuits as it produces
a specified output almost at the same instant
when input values are applied.
63
Example
64
Download