TOPIC 4.1. DATA TYPES
DIFFERENT DATA TYPES
- There are four primitive built-in data types
defined in the PASCAL language. These are:
(i)
INTEGER (Whole numbers)
(ii)
REAL (Numbers with a decimal point)
(iii)
CHARACTER (any ASCII character such as letter, number or 'special character'
like @, or #)
(iv)
BOOLEAN (A variable which can only hold the value true or false)
- TURBO PASCAL defines 5 different integer types,
which are listed below:
| type |
Range |
Format |
| shortint |
-128 .. 127 |
signed 8-bit |
| integer |
-32768 .. 32767 |
signed 16-bit |
| longint |
-2147483648 ..
2147483647 |
signed 32-bit |
| byte |
0 .. 255 |
unsigned 8-bit |
| word |
0 .. 65535 |
unsigned 16-bit |
- Each of these is stored in a different way in
the computer's memory.
- Therefore before we attempt to store any data in
memory, either by reading it in or by the use of an assignment
statement, we must tell the compiler what sort of data we intend to store in
any particular variable.
- This is done using the var declaration.
- For example:
| var |
| grade :
char; |
| mark1 :
integer; |
| mark2 :
integer; |
| average :real; |
- If a variable has been declared as an integer,
for example, we cannot store a character or a real number in it.
- In addition, we need some way of storing a
string of characters such as a student's name, and most versions of PASCAL
allow a string to be defined as, for example,
student_name : string;
which in Turbo Pascal allows up to 255 characters to be stored in student_name,
using a statement such as
student_name : 'Mary Jones';
- Note that the string has to be enclosed in
single quote marks.
RULES FOR IDENTIFIERS
The program name and the variable names are all
called as identifiers, and the following rules apply:
- they must always start with a letter,
- they can consist of letters, digits and the
underscore character
- uppercase and lowercase letters are treated as
identical
ASSIGNMENT STATEMENTS
- In Pascal a value or expression is assigned
to a variable using the symbol ':='.
- Here is an example of as assignment statement: -
daily_cost := cost_of_lunch + 2 * cost_of_drink;
- The following mathematical symbols are used in
constructing expressions:
|
Operator |
Operation |
Operand types |
Result type |
| + |
addition |
integer type; real
type |
integer type; real
type |
| - |
subtraction |
integer type; real
type |
integer type; real
type |
| * |
multiplication |
integer type; real
type |
integer type; real
type |
| / |
division |
integer type; real
type |
real type; real
type |
| div |
integer division |
integer type |
integer type |
| mod |
remainder |
integer type |
integer type |
- In addition, brackets ( ) are used wherever
necessary, or to clarify the meaning of the expression.
- The same rules of precedence apply as in
ordinary arithmetic; multiplication and division are performed before addition
and subtraction, and expressions in brackets are evaluated first.
READING IN DATA FROM A KEYBOARD
- Data is read into a Pascal program using either
a read or readln statement.
- We could read in three student marks from the
keyboard with the statement.
read (mark_1, mark_2, mark_3);
or
| read (mark_1); |
| read (mark_2); |
| read (mark_3); |
where mark_1, mark_2, mark_3 are integer values all
typed on one line, separated by at least one space.
- readln has the effect of skipping to a
new line after reading the variable.
read
(mark_1);
readln;
has the same effect as
readln(mark_1);
DISPLAYING DATA ON THE
SCREEN
- The write and writeln statements
are used to display data.
- As with readln, writeln causes a skip to
a new line after writing the data. Thus,
write('Average = ');
writeln(average);
has the same effect as
writeln('Average = ', average);
FORMATTING OUTPUT
- It is convenient to be able to control the
number of spaces used by the computer to print integers, and to be able to
specify the number of decimal places to be displayed in the case of a real
number.
- This is done by specifying the total field width
and number of places after the decimal point, separated by colons.
- Thus if the value of average is, say
74.0,
writeln('Average = ', average: 5 :1);
will print
Average = 74.0 (leaving one space after the = sign, for a total field
width of 5).
COMPOUND STATEMENTS AND
PUNCTUATION
- All the statements between begin and
end can be regarded as a single compound statement, with the semi-colon
acting as a separator between statements. (The significance of a
compound statement will become clearer later on when looping and branching
are covered).
- All statements between begin and end
need to be separated from each other, but there is no need to put a
semi-colon after the final statement before end.
- However it will not cause an error if you do so.
- Notice also that the last statement of the
program, end, has to be followed by a full-stop.
CHARACTER CODES AND
THEIR REPRESENTATION
- All types of data are represented in a
computer's memory as patterns of binary digits.
- When you press an 'a' on the keyboard, for
example, a particular pattern of 0s and 1s is stored in the keyboard buffer to
represent that character.
- Computer memory is commonly divided up into
groups of 8 bits known as bytes, with each byte able to store a single
character.
- Over the years, different computer designers
have used different sets of codes representing characters, with American
Standard Code for Information Interchange (ASCII) being commonly used on
personal computers.
- Originally only 7 bits were used, enough to
represent 128 different characters with 8th bit being used as parity
bit, but the extended ASCII character set uses all 8 bits to represent 256
different characters.
- Other codes such as Extended Binary Coded
Decimal Interchange Code (EBCDIC) are still in use on some mainframe
computers.
- The ASCII codes are shown below in Table 6.1.
|
Character |
ASCII |
| space |
00100000 |
| ! |
00100001 |
| " |
00100010 |
| # |
00100011 |
| $ |
00100100 |
| % |
00100101 |
| & |
00100110 |
| ` |
00100111 |
| ( |
00101000 |
| ) |
00101001 |
| * |
00101010 |
| + |
00101011 |
| , |
00101100 |
| - |
00101101 |
| . |
00101110 |
| / |
00101111 |
| 0 |
00110000 |
| 1 |
00110001 |
| 2 |
00110010 |
| 3 |
00110011 |
| 4 |
00110100 |
| 5 |
00110101 |
| 6 |
00110110 |
| 7 |
00110111 |
| 8 |
00111000 |
| 9 |
00111001 |
| : |
00111010 |
| ; |
00111011 |
| < |
00111100 |
| = |
00111101 |
| > |
00111110 |
| ? |
00111111 |
| @ |
01000000 |
| A |
01000001 |
| B |
01000010 |
| C |
01000011 |
| D |
0100100 |
| E |
01000101 |
| F |
01000110 |
| G |
01000111 |
| H |
01001000 |
| I |
01001001 |
| J |
01001010 |
| K |
01001011 |
| L |
01001100 |
| M |
01001101 |
| N |
01001110 |
| O |
01001111 |
| P |
01010000 |
| Q |
01010001 |
| R |
01010010 |
| S |
01010011 |
| T |
01010100 |
| U |
01010101 |
| V |
01010110 |
| W |
01010111 |
| X |
01011000 |
| Y |
01011001 |
| Z |
01011010 |
| [ |
01011011 |
| \ |
01011100 |
| ] |
01011101 |
| ^ |
01011110 |
| _ |
01011111 |
| ' |
01100000 |
| a |
01100001 |
| b |
01100010 |
| c |
01100011 |
| d |
01100100 |
| e |
01100101 |
| f |
01100110 |
| g |
01100111 |
| h |
01101000 |
| i |
01101001 |
| j |
01101010 |
| k |
01101011 |
| l |
01101100 |
| m |
01101101 |
| n |
01101110 |
| o |
01101111 |
| p |
01110000 |
| q |
01110001 |
| r |
01110010 |
| s |
01110011 |
| t |
01110100 |
| u |
01110101 |
| v |
01110110 |
| w |
01110111 |
| x |
01111000 |
| y |
01111001 |
| z |
01111010 |
| { |
01111011 |
| | |
01111100 |
| } |
01111101 |
| ~ |
01111110 |
| del |
01111111 |
|
Table 6.1: The ASCII character codes (excluding the extended character
set)
|
REPRESENTATION OF
INTEGERS
- When the characters 15863 are pressed on the
keyboard, the binary codes for 1, 5, 8, 6 and 3 are sent to the keyboard
buffer.
- If these digits represent, say, a telephone
number, they can be stored in this format in 5 consecutive bytes.
- If, on the other hand, the digits represent an
integer (whole number) that is to be used in a calculation, they need to be
converted to a binary integer.
- Our ordinary number system is called the
denary system and uses ten digits 0 to 0.
- In this system, the number 387 represents:
3 X 100 + 8 X 10 + 7 X 1
- This is a base 10 number system and as we move
from right to left, each digit is worth 10 times as much as the previous one.
TRANSLATING FROM BINARY
TO DENARY
- In the binary system, only the two digits 0 and
1 are used, and as we move from right to left, each digit is worth twice as
much as the previous one.
- Thus the binary number 01011101 can be set out
under column headings as follows:
| 128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
| 0 |
1 |
0 |
1 |
1 |
1 |
0 |
1 |
This represents 64 + 16 + 8 + 4 + 1
TRANSLATING FROM DENARY
TO BINARY
- To translate a denary number to binary, write
down the column headings and starting from the left hand end, figure out the
largest power of two that 'fits' into the given number.
- Put a 1 under the column heading, subtract this
amount from the number, and continue in the same way.
- For example, Translate the denary number 105 to
binary.
| Write the column headings |
128 |
64 |
32 |
16 |
8 |
4 |
2 |
1 |
| 64 'fits' |
0 |
1 |
|
|
|
|
|
|
| Subtract 64 from 105 leaving 41.
32 'fits' |
0 |
1 |
1 |
|
|
|
|
|
| Subtract 32 from 41 leaving 9, and
so on |
0 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
- A programmer generally has some control over how
many bytes are to be used to hold an integer value.
BINARY ARITHMETIC
- To add two binary numbers, remember that 'carry'
occurs as soon as you add 1 and 1 to make two, which is written not as 2 but
10.
- For example,
0 1 1 0 1 0 1 1 + 0 0 0 1 1 0 1 1 = (DIY- Test your self)
REPRESENTING NEGATIVE
NUMBERS USING SIGN AND MAGNITUDE
- There are two ways of representing negative
numbers.
- In the sign and magnitude method,
if the leftmost bit (the sign bit) is 1, the number is taken to be negative;
if it is 0, the number is positive.
- Thus 00000011 = 3; 10000011 = -3
- The problem with this method is that arithmetic
is inconvenient; for example adding the representations of +3 and -3 does not
result automatically in zero.
REPRESENTING NEGATIVE
NUMBERS USING 2'S COMPLEMENT
- A better way of representing negative numbers is
the 2's complement method.
- To translate a negative denary number to
binary using 2's complement:
1. Find
the binary value of the equivalent positive decimal number.
2.
Change all the 1s to 0s and vice versa.
3. Add 1
to the result.
- An even quicker way is to do the following:
1. Starting from the right, leave all the digits alone up to and including the
first 1.
2. Change all the other digits from 0 to 1 and 1 to 0.
- For example, 2's complement
of 01101011 is 10010101
- TIPS: Note that the sign bit is always 1 for a
negative number; The denary number -1 translates to 11111111 in an 8-bit byte;
and To perform binary subtraction, first find the 2's complement of the number
to be subtracted, and then add the two numbers.
THE HEXADECIMAL NUMBER
SYSTEM
- The hexadecimal number system uses 16 digits
0-9, A-F, with numbers being held in a base 16 format.
- Thus the hexadecimal number 1B6 represents 1 x
256 + 11 x 16 + 6 x 1 = 438.
- The advantage of this system is that it is very
easy to convert from binary to hexadecimal and if a programmer wants to
examine the contents of a portion of memory, for example, a hexadecimal memory
'dump' is very much easier to read than a string of binary digits.
- To convert from binary to hexadecimal, divide
the binary number into groups of 4 bits (starting from the right) and
translate each group of 4 bits into a hexadecimal digit.
- For example,
0110 1111 1001 1100 = 6 F 9 C
THE OCTAL NUMBER SYSTEM
- In the octal (base 8) system only the 8 digits 0
to 7 are used.
- To convert a binary number to octal, divide the
number into groups of 3 rather than groups of 4.
- This system was used on computers that had a
24-bit word divided into groups of 6 bits, instead of the now common, 16-, 32-
or 64-bit word divided into 8-bit bytes.
- For example,
001 101 111 = 1578
RESOURCES:
1) P M Heatcote & K R Bond,
[A Level Computing], Letts Educational Ltd, 1999.
2) P M Heatcote, [A Level
computing, 3rd Edition], Ashford Colour Press Ltd, 1996.