Presentation Slides
Presentation Slides
Representation of Data
Unit 1:
Bits, Integer Data Types Representation and operations
Dr. S. A. Bashir
Department of Computer Science
FUT Minna
CPT11 1
Outline
• Computer as a Binary Digital System.
• Number Bases
CPT11 1
Learning Outcomes
By the end of this unit, learners should be able to:
• Represent signed integers using sign magnitude, 1's complement, and 2's
complement.
• Calculate the number of different data values that can be represented with a
collection of n bits.
CPT11 1
Computer is a binary digital system
CPT11 1
Computer is a binary digital system.
CPT11 1
• Computers can process and store large amount of data and information. As such,
terms are used to refer to this large aggregate amount of bits or bytes.
8 bits = 1 Byte
In binary Kilo = 210 Mega = 220 Giga = 230 Tera = 240
CPT11 1
Digital Number System
• Binary: Base 2 0 1
• Octal: Base 8 0 1 2 3 4 5 7
• Decimal: Base 10 0 1 2 3 4 5 7 8 9
• Hexadecimal: Base 16 0 1 2 3 4 5 7 8 9 A=10 B=11 C=12 D=13 E=14 F=15
There are more than one way to express a number in binary. So 1010
could be -2, -5 or -6 and need to know which one.
CPT11 1
Unsigned Integers
• Unsigned integers are the non- 2 2 2 8
2 2 2 2 2 2
7 6 5 4 3 2 1 0
CPT11 1
Unsigned Integers cont.
• An n-bits can represent 2n different unsigned integer values that
range from 0 to 2n -1
For example 3 bits can represent unsigned integers from 0 to 7
CPT11 1
Converting Unsigned Integer to Binary
Unsigned integers are the non-negative integers. It is very straightforward to
represent them in binary.
1. Divide by two – remainder is least significant bit.
2. Keep dividing by two until answer is zero, writing remainders from right to
left.
3. Append zeros MSBs to fill the bit length of the representation.
Convert unsigned integer 4
Convert unsigned integer 104 to binary of 7bit size: to binary of 4 bit size:
X = 104ten 104/2 = 52 r 0 bit 0 4= 100 append 0 MSB 0100
52/2 = 26 r 0 bit 1
26/2 = 13 r 0 bit 2 Convert unsigned integer 15
13/2 = 6r 1 bit 3
to binary of 4 bit size:
6/2 = 3r 0 bit 4
3/2 = 1r 1 bit 5 15= 1111
X = 1101000two 1/2 = 0r 1 bit
4 bits filled don’t add 0 MSB
6
CPT11 1
Unsigned Integers
• Weighted positional notation 28 27 26 25 2 4 23 22 21 20
n 101 = 1 x 22 + 0 x 21 +1x 20 256 128 64 32 16 8 4 2 1
= 4 + 0+ 1 = 5
111 = 1 x 22 + 1 x 21 +1x 20 Convert any series of unsigned bits to unsigned
= 4 + 2+ 1 = 7 decimal using the positional values.
256 128 64 32 16 8 4 2 1
1 1 0 1 0 0 0 = 104
0 1 1 0 1 0 0 0 =104
1 1 1 1 = 15
1 0 0= 4
CPT111 1
Unsigned Integers cont.
• An n-bits can represent 2n different unsigned integer values that
range from 0 to 2n -1
For example 3 bits can represent unsigned integers from 0 to 7
CPT11 1
Unsigned Integers cont.
• An n-bits can represent 2n different unsigned integer values that
range from 0 to 2n -1
For example 3 bits can represent unsigned integers from 0 to 7
NUMBER OF BIT(s) USED FOR
REPRESENTATIONS
Unsigned
Integer 1Bits 2Bits 3Bits 4Bits 5Bits 6bits
0 0 00 000 0000 00000
1 1 01 001 0001 00001
2 - 10 010 0010 00010
3 - 11 011 0011 00011
4 - - 100 0100 00100
5 - - 101 0101 00101
6 - - 110 0110 00110
7 - - 111 0111 00111
8 - - - 1000 01000
CPT11 more more 1
Unsigned Integers Binary Arithmetic
CPT11 1
Signed Integers Representation
Signed integers comprise of both positive and negative integer values.
We know that given n bits, we can represent 2n distinct values. So, to represent both +ve and –ve integers:
n assign about half to positive integers (1 through 2n-1 - 1)
n assign about half to negative (- (2n-1 -1) through -1)
n that leaves two values: one for 0, and one extra
For example with 5 bits we can represent 2n I,e 25 (32) distinct values.
1
Signed Integer Representations
There are three different methods for representing signed integers:
i. Sign magnitude
ii. Ones’ complement
iii. Two’s complement.
Under the three methods, positive integers can be represented just like unsigned
zero in most significant (MS) to indicate +ve number.
1
Sign magnitude signed Integer Representation
To represent negative number set sign bit (of positive number.) to ‘1’ to indicate negative
1
Sign magnitude signed Integer Representations
To represent negative number set sign bit (of positive integer.) to ‘1’ to indicate negative
1
1’s complement signed Integer Representation
To represent negative number flip the bits (of positive number integer .) to obtain negative integer.
1
1’s complement signed Integer Representations
To represent negative number flip the bits (of positive integer .) to obtain negative integer.
1
Drawbacks of sign-magnitude:
n Arithmetic circuits are complex :Addition and subtraction require a consideration of both
the signs of the numbers and their relative magnitudes to carry out the required operation.
The addition of signed numbers using the sign-magnitude method is simple if the operands in
the addition are of the same sign, wherein the result takes on the sign of the operands. But in
case the operands have different signs, the process becomes complicated, and when used in
computers it requires logic circuits to compare and subtract the numbers. Since it is possible
to carry out the process without this circuitry, this method is not used in computer design.
How to add two sign-magnitude numbers? e.g., try 2 + (-3)
we may need to convert operands to decimal and result back to binary!
2= 0 0010 10010 (-2)
-3=1 0011 (-3 sign mag.) 10011 (-3)
? 0101= 10101 (-5) don’t add the sign bit
1
Drawbacks of 1’s complement representations:
n Arithmetic circuits are complex : The process of addition using the one's-complement
method may be simple or complicated, depending on the numbers being used. In certain
cases, an additional correction may need to be carried out to arrive at the correct answer.
How to add to one’s complement numbers? e.g., try 4 + (-3)
00100 +4
11100 -3
1 00000
1 Add carry
00001
To convert 1’s complement back to decimal:
If the number is positive (0 MSB) just convert it.
If the number is negative (1 MSB) take 1’s
complement and covert to decimal then add
negative to the decimal.
1. 0110 to decimal 6
2. 1001
i. take 1 comp. 0110
ii. Convert to decimal and put –ve sign = -6 1
Drawbacks of sign-magnitude and 1’s complement representations:
n Two representations of zero (+0 and –0): This is inconvenient because it is slightly more
difficult to test for 0 (an operation performed frequently on computers) than if there were a
single representation.
nIt also limit the range of value that can be represented to be -2n-1 -1 through 2n-1 – 1 with
n=5 -15 to 15 (31 distinct values, 1value is redundant i.e. -0 to complete 32 distinct values
expected when n=5)
Whereas 2 complement can represent -2n-1 through 2n-1 – 1 with n=5 -16 to 15 (32 distinct
values expected to be represented with 5 bits no redundant -0)
.
1
Two’s complement Integer Representations
Because of the aforementioned drawbacks, sign-magnitude and 1‘s complement representation is rarely
used in implementing the integer portion of the ALU. Instead, the most common scheme is twos
complement representation.
Two’s complement representation developed to make circuits easy for arithmetic.
for each positive number (X), assign value to its negative (-X), such that X + (-X) = 0 with “normal”
addition, ignoring carry out
CPT11 1
Two’s Complement Representation
• If number is positive or zero, just like sign-magnitude and 1’ compl.
• normal binary representation, zeroes in most significant (MS) bit(s)
• If number is negative,
• start with positive number
• flip every bit (i.e., take the one’s complement)
• then add one
CPT11 1
2’s complement signed Integer Representations
1
Two’s Complement representation Shortcut
• To take the two’s complement of a number:
• copy bits from right to left until (and including) the first “1”
• flip remaining bits to the left
011010000 011010000
100101111 (1’s comp)
(flip) (copy)
+ 1
100110000 100110000
CPT11 1
Two’s Complement Signed Integers to Decimal
• Method 1:
• MS bit is sign bit – it has weight –2n-1.
-23 22 21 20 -23 22 21 20
0 0 0 0 0 1 0 0 0 -8
0 0 0 1 1 1 0 0 1 -7
0 0 1 0 2 1 0 1 0 -6
0 0 1 1 3 1 0 1 1 -5
0 1 0 0 4 1 1 0 0 -4
0 1 0 1 5 1 1 0 1 -3
0 1 1 0 6 1 1 1 0 -2
0 1 1 1 7 1 1 1 1 -1
CPT11 1
Converting Binary (2’s C) to Decimal
Method 2:
1. If leading bit is one, take two’s complement to n 2n
get a positive number. 0 1
1 2
2. Add powers of 2 that have “1” in the 2 4
corresponding bit positions. 3 8
4 16
3. If original number was negative, 5 32
add a minus sign. 6 64
7 128
X = 01101000two 8
9
256
512
= 26+25+23 = 64+32+8 10 1024
= 104ten
Assuming 8-bit 2’s complement numbers.
CPT11 1
More Examples
X = 00100111two n 2n
0 1
= 25+22+21+20 = 32+4+2+1 1 2
= 39ten 2 4
3 8
4 16
Method 1: Method 2: 5 32
X = 11100110two X =11100110two 6 64
7 128
= 00011010 = - 8 256
= 24+23+21 = 16+8+2
128+64+32+4+2 9
10
512
1024
= 26ten = -128+102
X = -26ten X = -26ten
Assuming 8-bit 2’s complement numbers.
CPT11 1
Converting Decimal to Binary 2’s C
1. Find magnitude of decimal number. (Always positive.)
2. Divide by two – remainder is least significant bit.
3. Keep dividing by two until answer is zero, writing remainders from right to left.
4. Append a zero as the MS bit; if original number was positive, otherwise take two’s
complement.
X = 104ten 104/2 = 52 r 0 bit
0
52/2 = 26 r 0 bit
1
26/2 = 13 r 0 bit
2
13/2 = 6r 1 bit
3
6/2 = 3r 0 bit
4
3/2 = 1r 1 bit
CPT11 1
5
Operations: Arithmetic and Logical
• Recall:
a data type includes representation and operations.
• We now have a good representation for signed integers,
so let’s look at some arithmetic operations:
• Addition
• Subtraction
• Sign Extension
• We’ll also look at overflow conditions for addition.
• Multiplication, division, etc., can be built from these
basic operations.
• Logical operations are also useful:
• AND
• OR
• NOT CPT11 1
Addition
• As we’ve discussed, 2’s comp. addition is just binary addition.
Arithmetic is easy Subtraction = negation and addition
• Easy to implement in hardware
• assume all integers have the same number of bits
• ignore carry out
• for now, assume that sum fits in n-bit 2’s comlp. representation
01101000 (104) 11110110 Assuming 8-bit 2’s complement numbers.
(-10)
+ 11110000 (-16) + (-9)
01011000 (98) (-
19)
CPT11 1
Sign Extension
• To add two numbers, we must represent them with the same number
of bits.
• If we just pad with zeroes on the left:
4-bit 8-bit
0100 (4) 00000100 (still 4)
1100 (-4) 00001100 (12, not -4)
4-bit 8-bit
0100 (4) 00000100 (still 4)
1100 (-4) 11111100 (still -4)
CPT11 1
Overflow
• If operands are too big, then sum cannot be represented as an n-bit
2’s comp number. 8+9 =17 17 CANNOT BE
REPRESENTED IN 2’S COMPELEM
WITH 5BITS
01000 (8) 11000 (-8) FOR 5 BITS -16 TO 15
i.e. -2n-1 to 2n-1 -1
+ 01001 (9) + 10111 (-9)
10001 (-15) 01111 (+15)
A B A AND B A B A OR B A NOT A
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
• OR 11000101
• useful for setting bits 00000101
• OR with zero = no change
• OR with one = 1 OR 00001111
CPT11
00111010 1
XOR A B A XOR
B
Result is 1 if inputs are different 0 0 0 0
otherwise
0 1 1
Useful for checking if two numbers are 1 0 1
equal among other things
1 1 0
11000101
XOR 11000101
CPT11 00000000 1