Data-Representation

Download Report

Transcript Data-Representation

Outline
Data Representations
• Number Systems
• Hexadecimal, decimal and binary
numbers
• Logic operations on binary
numbers
• Binary subtraction and addition
Data Representations
• Most computers support a variety of data types.
• Numeric: – Integer: - Unsigned, signed
– Binary Coded Decimal (BCD)
– decimal, binary, octal, hexadecimal
– Fractions
– Floating point
• Non-numeric: – Character data
– Boolean
– String data
Number Systems
• main types of number system
representation
– Decimal,
– Binary,
– Octal,
– Hexadecimal
• Numbers represented as sequence of
digits, each carrying a certain weight.
Number Systems (cont)
The number of digits in a number system
is called the Base/radix
Number System
Base
Digits
Binary
2
0,1
Octal
8
0,…,7
Decimal
10
0,…,9
Hexadecimal
16
0,…,9,A,…F
Number Systems (cont)
• Decimal :
– commonly used by humans
• Binary :
– used by digital machines
• Octal, Hex:
– used to represent binary numbers in compact
form
Positional Number Systems
• Number systems are characterized by a
base or radix. e. g.
– Decimal number with base 10 (or radix 10)
– Binary numbers with base 2 (or radix 2)
• General: “radix R” or “base R”:
• Base or Radix of a number defines the
range of possible values a digit may have:
- e. g.
– 0 – 9 for decimal
– 0, 1 for binary
Positional Number Systems
• The general form of determining the value
of the number is:-
• The “d’s” are generally 0, 1, ... R
• Common notation: dddR
Binary Numbers
• A series of N bit binary number can
represent 2N different decimal numbers (0
up to 2N-1).
– For example, 4 Bits, we have 0-15
– 8 bits can represent decimal numbers 0-255,
• A group of four bits is called a nibble. A
group of 8 bits is called a byte
Octal/Hexadecimal numbers
• Octal uses digits 0 - 7
• Hexadecimal uses digits 0-9; A, B ,C ,D ,E
and F
Number Conversions
Decimal to binary
This is achieved by repeatedly dividing the
decimal number by 2 and noting the
remainder, until the quotient is 0
•If x/y=q+r, then x is called dividend, y is
called divisor, q is called quotient and r is
called the remainder
Number Conversions
MSB and LSB: 10010010
Binary number is formed by taking the last
remainder to be the Most Significant Bit
(MSB) and the first remainder to be Least
Significant Bit (LSB)
Remaindering Method
Example
Convert 1010 to binary
102 =5 + 0 (first remainder=0)
5  2 =2 + 1
2  2 =1 + 0
1  2 =0 + 1 (last remainder=1)
Therefore: 1010=10102
LSB
MSB
Fractional Decimal to Binary
Convert 0.375 decimal to binary
0.375x2=0.75 = 0.75 + 0
MSB
0.75x2 =1.50 = 0.50 + 1
0.50x2 =1.00 = 0.00 + 1
Therefore, 0.37510=0.0112
LSB
Extra example: 10.37510 = 1010. 0112
Decimal to hexadecimal
• Divide the number repeatedly by 16 and
note the remainder until the quotient is 0.
Last remainder becomes the MSB and the
first remainder becomes the LSB
Decimal to hexadecimal
Example
Convert 25610 to hexadecimal
256  16= 16+ 0
16  16 = 1 + 0
1  16 = 0 + 1
LSB
Therefore 25610=100HMSBwhere
hexadecimal
H
denotes
Decimal to hexadecimal
Example
Convert 12710 to hexadecimal
127  16 =7+F
7  16 = 0+7
LSB
MSB
Therefore 12710=7FH
Binary to decimal
Convert 10112 to decimal
10112=1x23+0x22+1x21+1x20
=8+0+2+1=1110
Binary to hexadecimal
To convert a binary number
hexadecimal, use the following steps:
to
• Write down the full binary number
• Split the number into 4 bit groups,
starting from the right
• Substitute the equivalent hexadecimal
digit for each group
Binary to Hex
Example
Convert 011111002 to hexadecimal
0111|1100 7|C
Therefore 011111002=7CH
Hexadecimal to decimal
The procedure is similar to converting a
binary number to decimal.
Example
Convert FFH to decimal
FFH=Fx161+Fx160 =15x16+15x1=255
Therefore FFH=25510
Hexadecimal to binary
• To convert an hexadecimal number to
binary, substitute an equivalent binary
number for each hexadecimal digit
Example
Convert F4H to binary
F|4=1111|0100=111101002
Therefore F4H=111101002
Hexadecimal to binary
Example
Convert 9A6H to binary
9|A|6=1001|1010|0110=1001101001102
Therefore 9A6H=1001101001102
Binary to Octal/Hex
•
•
•
•
•
•
•
•
Binary
000
001
010
.
.
.
111
Octal
0
1
2
.
.
.
7
•
•
•
•
•
•
•
•
•
Binary
0000
0001
0010
.
1000
1001
1010
1111
Hex
0
1
2
.
8
9
A
F
Octal/Hex to Decimal
• Use binary as intermediate
Octal
Binary
Hexadecimal
Decimal
Logical operations on binary
numbers
Define
• 0 as false and
• 1 as true,
• Truth tables for AND, OR and NOT logical
operators can be easily constructed
Logical operators table
A OR B A AND B NOT A
A
B
0
0
0
0
1
0
1
1
0
1
1
0
1
0
0
1
1
1
1
0
Logical Operations
Example :
Perform 76H OR 93H
76H OR 93H = 011101102 OR 100100112=
111101112 =F7H
Therefore, 73H OR 93H = F7H
Logical Operations
Example :
Perform 76H AND 93H
76H AND 93H = 011101102
100100112= 000100102 =12H
Therefore, 73H AND 93H = 12H
AND
Logical Operations
Example :
Perform NOT 76H
NOT 76H= NOT 011101102 = 100010012 =
89H
Therefore NOT 76H = 89H
Arithmetic Operations
• 3 ways of representing binary
numbers in a machine
– signed binary
– 1’s complement
– 2’s complement
Addition And Subtraction
• Signed numbers need more complex
circuits than complemented numbers
• Modern CPUs use complemented
numbers
• Subtractions (and multiplication) are
done using additions
Addition and subtraction
• Case 1: All positive numbers
– straight forward procedure if out-of-range
results are taken care of.
• Examples:
– 5510 + 2710
– 17710+8410
• Case 2: Some negative numbers
– use complemented numbers
Arithmetic Operations
• 2’s complement
Nn(r) = rn - Nn
• 1’s complement (diminished radix)
Nn(r-1) = rn - 1 - Nn
Nn(r-1) = 1 + Nn(r)
Arithmetic Operations
• The 1’s complement of a binary number is
formed by inverting all the bits
• The 2’s complement is formed by adding 1
to the 1’s complement
Signed Numbers
• The left most bit of complemented
numbers is reserved for the sign
0 = positive
1 = negative
• Negative numbers are obtained by using
1’s or 2’s complement form
Binary Addition
Example 4.17
Find 27H+56H
27H = 001001112
+
56H = 010101102
-----------------------011111012= 7DH
-------------------------Therefore 27H+56H=7DH
Binary subtraction
• On most computers, negative numbers are
represented using 2's compliment.
To find 2's
compliment, first find 1's compliment by flipping the
bits (0 becomes 1 and 1 becomes 0), then add a one
to the 1's compliment
• Use 2's compliment in making subtraction. Note: if
D=X-Y, then X is known as Minuend, Y is known as
Subtrahend, and D is known as Difference
• Balance number of bits for minuend and Subtrahend
One’s Complement
• One method of representing negative
numbers
• using 3 bits we have
000
0
100
-3
001
1
101
-2
010
2
110
-1
011
3
111
0
• The ones compliment is found by changing the
zeros for ones and ones for zeros when forming
the negative number. The following is a list of
the numbers in ones compliment that can be
formed using only four digits (remember that
the left most digit is the sign digit).
• O 0000
0
1111
• 1 0001
-1 1110
• 2 0010
-2 1101
• …………………………..
• 6 0110
-6 1001
• 7 0111
-7 1000
One’s Complement
• Note that there are two representation
for zeros (all 0’s and all 1’s)
• ambiguous when testing if a number is
zero
• the representation is normally not used
One’s Complement
• How to subtract x from y using n-bits
– Negate x using 1's complement.
– Add -x and y.
– If the sum exceeds n bits, add the extra bit to
the result.
– If the sum does not exceed n bits, leave the
result as it is.
One’s Complement
• Examples:
– subtract 710 from 110 using 1's complement
– 011012 (1310) - 010012 (910) = ?
Twos Compliment
• The twos compliment is obtained by
forming the ones compliment first and then
adding one.
• e.g.
+3 =
0011
• ones compliment
1100
• Add one
1101
• To go back to +3
Two’s Complement
• In two’s Overflow bits are ignored
• positive numbers are treated just as in 1’s
complement (1 to 2n-1-1)
Two’s Complement
• Overflows “wrap-around” from +ve to -ve
numbers hence -(-4) = -4
• with n bits the range represented is -2n-1 to
+2n-1-1
000
0
100
-4
001
1
101
-3
010
2
110
-2
011
3
111
-1
Two’s Complement
• 2’s complement has no ambiguity in
representing zero
• addition of a number and its complement
equals zero
Two’s Complement
• Use 2’s complement to perform the
following arithmetic:
– 4310 - 2510
– 5710 - 9610
– 4710 - 3310
– 8810 - 7310
Overflow Conditions
• In 1’s/2’s complement addition/subtraction:
– if the operands are of the same sign and the
sign of the result is different then an overflow
has occurred and the result is invalid
• 1’s complement addition/subtraction :
– if a carry of 1 is produced (illusory digit) it
must be added to the result at the LSB in
order get the correct result (so called “endaround carry”)
Example 4.19
Find 63H-87H
87H=100001112
-87H=011110012 (2’s compliment of subtrahend)
63H =011000112
Therefore 63H-87H= 011000112 + 011110012
=1101|11002
No carry, MSB=1, which means that the result is
negative.
To get the final answer, find 2's
compliment of the sum.
2's compliment of 11011100 is 00100100=24H
Therefore,63H-87H=-24H