You are on page 1of 43

Lesson Plan

Title: DATA TYPES AND DATA REPRESENTATION Ref. No: Lecture


12, 13, 14
Target Population: CSC 103 Duration: 180 min
Aims: To introduce students to data types in C language and their computer
representation
Learning outcomes: At the end of the class, the students will be able to: (a)
understand the fundamental data types used in C programming, and (b) how
to store positive and negative integers using various representation methods.

Content Method/ Resource or Time


Technique Aid
Introduction: Attendance, Lecture; Q/A White board, 30 min
Rapport building, Summary Multimedia
of the previous lecture by a projector
student, Questions from the
previous class, Pre-
assessment, Lecture outline
Development: Lecture, Q/A, White board, 120 min
1. Definition of data and Discussion Multimedia
information projector
2. Fundamental data types
3. Representation of integers using:
(a) unsigned integer method, (b)
sign-magnitude method and (c)
two’s complement method.
Conclusion: Lecture, Q/A White board, 30 min
1. Recap of main points Multimedia
2. Students’ Feedback & Answer projector
3. Assessment of students’
Learning Outcomes
4. References/Suggested Reading
(Lecture note) and Forward
planning for next lecture

2
Definitions of Data and Information
Data: Data usually refers to raw data, or
unprocessed data. It is the basic form of data
that hasn’t been analyzed or processed in any
manner.

Information: Once the data is analyzed, it is


considered as information.
Some differences between data and information

• Data is the raw material. Information is the product.


• Data is unprocessed facts and figures. Information is
processed data.
• Data is used as input for the computer system.
Information is the output of data.
• Data doesn’t depend on Information. Information
depends on data.
• Data is not specific. Information is specific.
• Data doesn’t carry a meaning. Information must carry a
logical meaning.
At its most rudimentary level, computer data
is a bunch of ones and zeros, known
as binary data. Because all computer data is in
binary format, it can be created, processed,
saved, and stored digitally. This allows data to
be transferred from one computer to another
using a network connection or various media
devices. It also does not deteriorate over time
or lose quality after being used multiple times.
Data types
• Before we enter data into a computer system,
we usually need to tell the computer
what type of data it is. This is because the
computer stores and processes different types
of data in different ways.

• The most common data types


include  integer, floating-point number and
character.
An integer is a whole number - it has no decimal or
fractional parts. Integers can be
either positive or negative.

Examples
• 3
• 40
• 589
• 4356
• -15
• -87690, etc
• A number with a decimal is referred to as
a floating point number. Examples: 5.0,
132.57, etc.
• Character data types include letters, numerical
digits, common punctuation marks (such as ‘.’
or ‘-’), and whitespace.
Data Representation
Data representation considers how a
computer uses numbers to represent data
inside the computer. Computers store lots of
different types of information such as numeric
data, text, images, audio, and video. 
Computers use a variety of different codes. Some
are used for numbers, others for text, and still
others for sound and graphics.

The codes used by computers are based on the binary


number system (base two) instead of the more
familiar (for the moment, at least!) decimal system.
Computer Representation of
Positive and Negative Integers
• Computers use a fixed number of bits to represent an
integer. The commonly-used bit-lengths for integers are 8-
bit, 16-bit, 32-bit or 64-bit. Besides bit-lengths, there are
two representation schemes for integers:
• Unsigned Integers: can represent zero and positive integers.
• Signed Integers: can represent zero, positive and negative
integers. Three representation schemes had been proposed
for signed integers:
– Sign-Magnitude representation
– 1's Complement representation
– 2's Complement representation
• Representation of positive whole numbers

We can represent any number, however large,


in binary. However, we can only store numbers
between 0 and 255 in one byte. This is
obviously rather restrictive since it's not
dealing with large integers, negative numbers
or decimal numbers.
• An n-bit pattern can represent 2^n distinct
integers. An n-bit unsigned integer can
represent integers from 0 to (2^n)-1, as
tabulated below:
n Minimum Maximum

8 0 (2^8) – 1 = 255; the largest number

represented just in one byte.

16 0 (2^16) – 1 = 65,535

32 0 (2^32) – 1 = 4,294,967,295

64 0 (2^64) – 1 = 18,446,744,073,709,551,615
The larger the number we wish to represent,
the more bits we require. Thus we can
represent more numbers with 16 bits than we
can with 8 bits.

Usually, a given computer uses a fixed number


of bits for storing integers, so we use terms as
8-bit integers, 16-bit integers and 32-bit
integers. Whatever the size, the principles
remain the same; the only difference is that
with more bits we can store a wider range of
numbers.
Sign and Magnitude
In this representation, one bit is used for sign of
the number: 0 for positive, 1 for negative. The
remaining bits are used to store the binary
equivalent of the number (without the sign).
Thus the representation of 5 in 4-bit
representation is:
0 1 0 1
Sign bit Binary equivalent of 5
The representation of -4 is:
1 1 0 0

Sign bit Binary equivalent of -4


Since the maximum value that can be stored
with 3 bits is 7, the range of integers which
can be represented is:
-7 to +7.

One disadvantage of this method is that there


are two ways to represent 0: 0000 and 1000. A
computer using this method would need
special circuits to avoid confusion between
these two possible representations.
Exercises:
• Using sign and magnitude, what range of
integers can be stored using (a) 8 bits, (b)
using 16 bits?
• Using 6 bits, give the sign and magnitude
representation of (a) 18 (b) 31
(c) -13 (d) -1 (e) - 12
Two’s complement representation

• Most widely used method of representation.


• Positive numbers are represented as they are (simple
binary).
• To get a negative number, write the positive number
in binary, then change all 0’s to 1’s and 1’s to 0’s. Then
add 1 to the number.
• ·Example : +7 would be 0111 in 4-bit 2’s complement.
• To represent –5 we take +5 (0101) and then invert the
digits (1010) and add 1 (1011). –5 is thus 1011.
• Suppose you already have a number that is in two’s
complement representation and want to find its value
in binary. If the number starts with a 1 it is a negative
number. If it starts with a 0 it is a positive number.
• If it is a negative number, take the 2’s complement
of that number. You will get the number in ordinary
binary. The sign you already know. Let’s take 1101.
Take the 2’s complement and you get 0011. Since it
started with a 1, it was
negative and the value is 0011 which is 3. The
number represented by 1101 is –3 in 2’s complement.
Let’s see how 2’s complement representation is better:
• If we add +5 and -5 in decimal we get 0.
• Let’s add them in 4-bit signed-magnitude. +5 is 0101
and –5 is 1101. On adding we get 10010. That is not
zero.
• Let’s do the same thing in 2’s complement. Adding
0101 (+5) and 1011 (-5) gives 10000. If we discard
the carry of 1 we get 0000 – i.e. 0.
Thus addition works out ok for negative numbers in
2’s complement whereas it doesn’t in sign
magnitude.
• Similarly, you can show that multiplication and
subtraction all work in 2’s complement but do
not in other representations. The other
number systems require much more
complicated hardware to implement basic
mathematical functions. i.e.
add/subtract/multiply.
Two’s complement representation
Before we look at two’s complement
representation, we need to know what is
meant by one’s complement.

One’s complement
Using 4 bits, say, the one’s complement of a 4
bit binary number is found by changing 0s to
1s and 1s to 0s. For example, the 4-bit one’s
complement of 0110 is 1001.
Note that if we were asked for the 4-bit one’s
complement of 112, we would need to write 11 as
00112 (expand 112 to 4 bits) and then change 0s to 1s
and 1s to 0s, giving 11002 as the 4-bit one’s
complement of 112.
It is important to know that it is wrong to simply say
‘one’s complement of 011’; we must specify how many
bits are involved by saying, for instance, ‘the 4-bit one’s
complement of 011’. It is interesting to note that:
 the 3-bit one’s complement of 011 is 100, and
 the 4-bit one’s complement of 011 is 1100.
Things to notice:
1. Any negative number will have a 1 in the
MSB.
2. There are 2 representations for 0:
0000 and 1111.
Two’s complement
A variation on 1's complement that does NOT
have 2 representations for 0. This makes the
hardware that does arithmetic faster than for
the other representations.
The two’s complement of a binary number is obtained by
adding 1 to its one’s complement. For example, the 4-bit
one’s complement of 0110 is 1001. Therefore, the 4-bit
two’s complement of 0110 is 1001+1 = 1010.
Suppose we wanted to store integers using 4-bits.
Remember that with 4 bits, we can have 16 different
binary sequences from 0000 to 1111. Therefore we can
represent 16 different integers using 4 bits. Of the 16
different patterns, 8 begin with 0 and 8 begin with 1.
Those beginning with 0 represent positive integer and
those beginning with 1 represent negative integer.
Problem 1: Find the two-byte integer
representation of 105

2 bytes = 2x8 bits =16 bits


16 bits are like 16 storage places. Each storage
place holds either a 0 or 1.
Computer is going to hold 105 as a binary
number of length 16. For this you need to
convert 105 to binary number.
(105)10 = (1101001)2
(105)10 = (0000000001101001)2
Problem2: Find the 4-byte integer
representation of 235.

Problem 3: Find the 2 byte integer


representation of -234.
Convert 234 to binary.
(234)10 = (11101010)2
(234)10 = (00000000111011010)2
0000000011101010
Flip bits: 1 1 1 1 1 1 1 1 0 0 0 1 0 1 0 1
Add 1: + 1
___________________________
1111111100010110
(-234)10 is represented on the computer as:
1111111100010110
Problem: Find the decimal number represented by the
16 bit computer representation:
(1) 0000 0000 0001 1010
(2) 0010 1010 0111 1111

Problem: Find the decimal number represented by the


16 bit computer representation:
(1) 1111 1111 0111 1111
Flip the bits: 0000 0000 1000 0000
Add 1 + 1
______________________________________
0000 0000 1000 0001
(0000 0000 1000 0001)2 = (12 9)10
Ans: -129

• what decimal value does the two's


complement 110011 represent?
• How many integers can a 2-byte computer
allocation store? What is the range of these
integer values?

Number of integers: 216 = 65536


Range: 216 /2 = 215 to 215-1
-32768 to 32767
• How many integers can a 4-byte computer
allocation store? What is the range of these
integer values?
• How many integers can a 8-byte computer
allocation store? What is the range of these
integer values?
Assuming that a total of n bits is reserved for
storing integers, the range of numbers that can
be represented in each of the three methods is
as follows:

Unsigned method: 0 to 2n – 1
Sign-magnitude method: - (2n-1 – 1) to (2n-1 – 1)
Two’s complement method: -2n-1 to 2n-1 – 1
1. What decimal number does the two’s
complement number 1010 represent?
2. What decimal number does the two’s
complement number 1110 represent?
3. What decimal number does the two’s
complement number 1010 represent?
4. Using 2‘s complement, represent -15 in 8
bits.
5. Using 2‘s complement, represent -240 in 8
bits.
• Which of these represents -3 as a five bit two’s
complement number?
A. 00011
B. 10011
C. 11100
D. 11101
• Given a 5 bit two’s complement number, what
is the largest positive value you can represent?

A. 5
B. 7
C. 15
D. 31

You might also like