Data Representations in Computer Programs |
Data Representations in Computer Programs
Representations and Conversions
Integers
A single
byte can store an integer that takes on a value from 0 to 255. The
interpretation of the bits in the byte uses normal binary representations
for numbers. You might not see this representation often.
unsigned char x;
A single
bytecan also store an integer that takes on a value from -128 to +127.
In this case, one bit in the byte is used as a sign bit. In C, an integer
of this type is declared using:
char x;
or
short int x;
Larger integers can be stored
using two bytes. Two bytes can store 216 - 1 or 65536
numbers from 0 to 65535. The numbers represented can run from 0 to 65535.
In C, an integer of this type is declared using:
unsigned int x;
Signed integers take two bytes
and use one bit to store the sign (plus or minus), and have a range from
-32768 to +32767, and are declared like this:
int x;
NOTE: In some cases, the
declaration above will give you an integer that takes four bytes, and which
can have much larger values.
Finally, there is a long
integer that is used for large integers. A long integer takes four bytes,
and will handle integers from -2,147,483,648 to -2,147,483,647. The form of
the declaration is:
long int x;
Now, none of these
representations will handle something like 2.59 - a number that is not an
integer. You need to use a floating point representation for those numbers.
Data Type
|
Number of
Bytes |
Number of
Bits |
Float
|
4
|
32
|
Double
|
8
|
64
|
Long Double
|
12
|
96
|
float
4 32
double 8
64
long double 12
96
Floating Point Numbers
IEEE has written a
standard for floating point numbers. The simplest IEEE representation uses a 32
bit word (four bytes) and looks something like this:
S |
E |
E |
E |
E |
E |
E |
E |
E |
S |
F |
F |
- |
- |
F |
F |
F |
F |
F |
F |
F |
F |
F |
F |
0 |
1 |
|
|
|
|
|
|
8 |
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
- This representation is short
a few bits - just to be sure that it will "fit".
- There are two sign bits in
the representation. The exponent can be positive or negative, and the
mantissa can have either sign as well.
This representation can be
difficult to unravel, but S is the sign bit, and the eight bit "E" number is a
signed integer representing the exponent. The "F" number determines the
mantissa and is actually the floating point part in a 1.X representation, where
X is the number represented by F. A four bit floating point number has a
declaration like this.
What you need to remember is
that a floating point number is stored as an exponent and a mantissa along with
a sign. That means that - in the four byte representation - there are only 24
bits devoted to the mantissa (significant figures) and you have to remember not
to overestimate how precise the representation is.
If you want a more precise
representation of a number, you can use a double precision floating point
representation that uses eight (8) bytes. The declaration is:
ASCII coded numbers
Numerous instruments
transmit measurement data using ASCII coded numbers. In an ASCII coded number.
For example, a measurement that yields a value 3.145 would be transmitted to a
computer with the following sequence of characters.
- An ASCII character 3,
- An ASCII character .,
(That's a period!)
- An ASCII character 1,
- An ASCII character 4,
- An ASCII character 5,
- A separator that could be
either one of these:
- If the next piece of
data is stored in another column, then the next character in the
file should be an ASCII character Tab, (A tab character) That would
produce a "Tab-delimited" file since the delimiter is a tab
character.
- There are times
when "Comma-delimited" files are used. In that situation, the
tab character is replaced by a comma.
- If the next piece of
data starts another row, then it is often the case that two
characters are used. Those characters are an ASCII character CR,
(Carriage return) and an ASCII character LF, (Line Feed). (In
manual typewriter days, the carriage return took the carriage back
to the first position at the left of the page, and the line feed
advanced to the next line.)
T.
|