0% found this document useful (0 votes)
7 views40 pages

Data RepresentationM3 - Uni1 - 1 To 1 - 3 Slides

This document covers the representation of data at the machine level, focusing on bits and integer data types. It explains the binary digital system, number bases, and methods for representing unsigned and signed integers, including operations like addition and subtraction. The learning outcomes include the ability to represent integers in binary, perform conversions between number systems, and understand the implications of different integer representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Data RepresentationM3 - Uni1 - 1 To 1 - 3 Slides

This document covers the representation of data at the machine level, focusing on bits and integer data types. It explains the binary digital system, number bases, and methods for representing unsigned and signed integers, including operations like addition and subtraction. The learning outcomes include the ability to represent integers in binary, perform conversions between number systems, and understand the implications of different integer representations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Module3: Machine Level

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

• Different kinds of data we need to represent.

• Unsigned Integer representation

• Signed Integer representation

• Operations on integer type: addition, subtraction, sign-extension,


logical operation

CPT11 1
Learning Outcomes
By the end of this unit, learners should be able to:

• Represent unsigned integers in binary form.

• 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.

• Perform addition and subtraction in 2's complement.

• Perform logical operations on represented binary data.

CPT11 1
Computer is a binary digital system

• Computer is an electronic device that works by controlling the flow of


voltages.
Rather than manipulating continuous voltage changes, computer uses two
level of voltages.
• Easy to recognize two conditions:
1. presence of a voltage – we’ll call this state “1”
2. absence of a voltage – we’ll call this state “0”

CPT11 1
Computer is a binary digital system.

Therefore the basic unit of information in a computer is


the binary digit, or bit. Which can be in one of two
states ‘’ 0 ‘’ or ‘’1’’.

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

• Positional number system


• 32810 = 3×102 +2×101 + 8×100 =32810
• 1012= 1×22 + 0×21 + 1×20 =510

• 638 = 6×81 + 3×80 = 51 10

• A116= 10×161 + 1×160 = 16110


Binary → hex/decimal/octal
conversion
• Conversion from binary to octal/hex
• Binary: 10011110001
• Octal: 10 | 011 | 110 | 001=23618
• Hex: 100 | 1111 | 0001=4F116

• Conversion from other bases to decimal


• 1012= 1×22 + 0×21 + 1×20 = 510
• 63.58 = 6×81 + 3×80 + 4×8–1 = 51.510
• A116= 10×161 + 1×160 = 16110
Decimal→ binary/octal/hex
conversion

• Each successive divide liberates an LSB (least significant bit)


What kinds of data do we need to represent?

• We use binary digits to represents different kinds of data which


include:
• Numbers – signed integers, unsigned integers, floating point...
• Text – characters, strings, …
• Logical – true, false
• Images – pixels, colors, shapes, …
• Sound – wave forms
• Instructions: program
Data type: representation of and operations that can be performed on a
type within the computer

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

negative integers. It is very 256 128 64 32 16 8 4 2 1


straightforward to represent them
in binary. Convert any series of unsigned bits to unsigned decimal
• Weighted positional notation using the positional values.

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
= 4 + 2+ 1 = 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

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

• Base-2 addition – just like base-10!


n add from right to left, propagating carry

Subtraction, multiplication, division: is performed similarly as decimal


integer math!

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.

If it were for unsigned integers the values will be 0 to 2n -1 i.e. 0 to 25 -1 i.e 0 to 31

However, to represent signed integers the 25 (32) distinct values:


n assign about half to positive integers (1 through 25 -1 -1) (1 to 15)
n assign about half to negative (-(2n-1 -1) through -1) (-15 to -1)
n that leaves two values: one for 0, and ne extra

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

NUMBER OF BIT(s) USED FOR REPRESENTATIONS


Signed Integers 1Bits 2Bits 3Bits 4Bits
+ + - + - + -
0 -0 0 - 00 10 000 100 0000 1000
1 -1 1 - 01 11 001 101 0001 1001
2 -2 010 110 0010 1010
3 -3 011 111 0011 1011
4 -4 0100 1100
5 -5 0101 1101
6 -6 0110 1110
7 -7 0111 1111

1
1’s complement signed Integer
To represent negative number flip the bits (of positive number integer .)
Representation to obtain negative integ

5bits Unsigned 1’s Complement

1
1’s complement signed Integer
Representations
To represent negative number flip the bits (of positive integer .) to obtain negative integer.

NUMBER OF BIT(s) USED FOR REPRESENTATIONS


Signed Integers 1Bits 2Bits 3Bits 4Bits
+ - + - + - + -
0 -0 0 00 11 000 111 0000 1111
1 -1 1 01 10 001 110 0001 1110
2 -2 010 101 0010 1101
3 -3 011 100 0011 1100
4 -4 0100 1011
5 -5 0101 1010
6 -6 0110 1001
7 -7 0111 1000

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

1’s complement arithmetic 2’s complement arithmetic To convert to 2’s


complement:
00101 (5) 00101 Take(5)
1’s complement
and add 1
+ 11010 (-5) + 11011 (-5)
11010 + 1
11111 (-0) 00000 (0)
= 11011

To convert to 2’s
01001 (9) 01001 (9) complement:
Take 1’s complement
+ 10110 (-9) + 10111 (-9) and add 1
11111 (-0) 10110 + 1
00000 (0)
= 10111
CPT11 1
Two’s complement Integer Representations

5bits Unsigned 2’s Complement 5bits Unsigned 2’s Complement


• Range of an n-bit number:
-(2n-1) through 2n-1 – 1.
• The most negative number
–(2n-1) has no positive counterpart

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

00101 (5) 01001


(9)
11010 (1’s comp)
(1’s comp)
+ 1 + 1
11011 (-5) (-
CPT11 1
9)
2’s complement signed Integer
Representations

NUMBER OF BIT(s) USED FOR REPRESENTATIONS


Signed Integers 1Bits 2Bits 3Bits 4Bits
+ - + - + - + -
0 -0 0 00 - 000 0000
1 -1 1 01 11 001 111 0001 1111
2 -2 10 010 110 0010 1110
3 -3 011 101 0011 1101
4 -4 100 0100 1100
5 -5 0101 1011
6 -6 0110 1010
7 -7 0111 1001
-8 1000

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 (flip) (copy)
100101111 (1’s comp)
+ 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

2. Add powers of 2 that have “1” in the 1


2
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 256
9 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
ethod 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 512
10 1024
= 26ten = -
128+102
X = -26ten
Assuming 8-bit 2’s complement numbers. X = -26ten
CPT11 1
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) Assuming 8-bit 2’s complement numbers.
11110110 (-10)
+ 11110000 (-16) +
(-9)
01011000 (98) (-
19)

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 FOR 5 BITS -16 TO 15
i.e. -2n-1 to 2n-1 -1
(-8)
+ 01001 (9) + 10111
(-9)
• We have overflow if:
10001 (-15)
signs of both operands
• 01111 (+15) are the same, and
• sign of sum is different.
• Another test -- easy for hardware:
• carry into MS bit does not equal carry out
CPT11 1
Logical Operations
• Operations on logical TRUE or FALSE
• two states -- takes one bit to represent: TRUE=1, FALSE=0

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

• View n-bit number as a collection of n logical values


• operation applied to each bit independently
CPT11 1
Examples of Logical Operations
• AND
• useful for clearing bits 11000101
• AND with zero = 0
• AND with one = no change AND
00001111
• OR
• useful for setting bits 00000101
11000101
• OR with zero = no change
• OR with one = 1
OR
00001111
• NOT NOT
• unary operation -- one argument
11001111
11000101
• flips every bit

CPT11
00111010 1
XOR A B A XOR
B
Result is 1 if inputs are different 0
otherwise
0 0 0
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

You might also like