Docsity

Understanding Data Types: Integers, Floats, and Base 2 Storage, Study notes of Programming Paradigms

Toggle sidebar
bg1
Data Types and Representations
Since we will be performing numerical calculations using the C compiler, it is
useful to know how the different data types are stored. Because we have 10 fingers
and 10 toes, we have been brought up expressing integers and real numbers in base
10. However, the computer doesn’t have fingers or toes. The easiest method of arith-
metic operations for computers is in binary, base 2. We will represent integers and
real numbers in the following notation: the number in parenthesis with the base as a
subscript: (number)base or (yyy.xxxx)base.
The main data types that we will be using will be integers, long integers, float,
and double precision.
char types
Computers do numerical operations and store data in base 2. The smallest data
type is a byte, which is 8 bits. Each bit can be a 0 or 1. The simplest data type is an
unsigned char. The unsigned char type is one byte long, and the number it repre-
sents are just the 8 bits in base 2. For example, (00000000)2is zero, and (11111111)2
in base 2 is (255)10 in base 10. So the unsigned char is an integer between 0 and
(255)10.
The next simplest type is the char. The first bit represents the sign of the integer,
with 0 being +, and 1 being -. The next 7 bits are the integer in base 2. The smallest
integer is 11111111 which is (−12710 in base 10. The largest integer is 01111111,
which is (+127)10 in base 10. So the char type is an integer between (−127)10 and
(+127)10.
int type
The int type is stored in four bytes, or 32 bits. The first bit represents the sign
of the integer, with 0 being + and 1 being -. The next 31 bits are the integer in base
2. The largest integer is 011 ···11 which is +231 −1 = +(2147483647)10. The most
negative integer is −(2147483648)10. So the int type between −(2147483648)10 and
+(2147483647)10.
The long int type also stores integers in four bytes, or 32 bits. Storage is similar
1
pf3
pf4

Partial preview of the text

Download Understanding Data Types: Integers, Floats, and Base 2 Storage and more Study notes Programming Paradigms in PDF only on Docsity!

Data Types and Representations

Since we will be performing numerical calculations using the C compiler, it is useful to know how the different data types are stored. Because we have 10 fingers and 10 toes, we have been brought up expressing integers and real numbers in base

  1. However, the computer doesn’t have fingers or toes. The easiest method of arith- metic operations for computers is in binary, base 2. We will represent integers and real numbers in the following notation: the number in parenthesis with the base as a subscript: (number)base or (yyy.xxxx)base.

The main data types that we will be using will be integers, long integers, float, and double precision.

char types

Computers do numerical operations and store data in base 2. The smallest data type is a byte, which is 8 bits. Each bit can be a 0 or 1. The simplest data type is an unsigned char. The unsigned char type is one byte long, and the number it repre- sents are just the 8 bits in base 2. For example, (00000000) 2 is zero, and (11111111) 2 in base 2 is (255) 10 in base 10. So the unsigned char is an integer between 0 and (255) 10.

The next simplest type is the char. The first bit represents the sign of the integer, with 0 being +, and 1 being -. The next 7 bits are the integer in base 2. The smallest integer is 11111111 which is (− 12710 in base 10. The largest integer is 01111111, which is (+127) 10 in base 10. So the char type is an integer between (−127) 10 and (+127) 10.

int type

The int type is stored in four bytes, or 32 bits. The first bit represents the sign of the integer, with 0 being + and 1 being -. The next 31 bits are the integer in base

  1. The largest integer is 011 · · · 11 which is +2^31 − 1 = +(2147483647) 10. The most negative integer is −(2147483648) 10. So the int type between −(2147483648) 10 and +(2147483647) 10.

The long int type also stores integers in four bytes, or 32 bits. Storage is similar

to the int type with the first bit representing the sign. The most negative integer is therefore 111 · · · 11 which is −(2^31 − 1) = −(2147483647) 10. The most positive integer is 0111 · · · 111 = +(2147483647) 10. So the long int type is an integer between −(2147483648) 10 and +(2147483647) 10.

float type

We are used to expessing real numbers in decimal notation: (0.1) 10 = 1/10 = 10−^1 , (0.01) 10 = 1/100 = 10−^2 , etc. We are not as familiar expressing real numbers in base

  1. Using the same idea as in base 10, (0.1) 2 = 1/2 = 2−^1 , (0.01) 2 = 1/4 = 2−^2. In general, real numbers can be expressed in base 2 notation as:

(x 2 x 1 x 0 .x− 1 x− 2 · · ·) 2 = x 222 + x 121 + x 0 + x− 12 −^1 + x− 22 −^2 · · · (1)

Now we are ready to understand how the computer will store real numbers.

The float data type uses 4 bytes, or 32 bits, to store a real number. The convention that has been chosen is to have the first bit represent the sign of the number, the next 8 bits represent the exponent in base 2, and the last 23 bits the mantisa of the number in base 2. More specifically:

sc 1 c 2 · · · c 8 f 1 f 2 · · · f 23 → (−1)s(1.f ) 2 2 c−^127 (2)

where s, the ci, and the fi correspond to the ”zero” or ”one” value of the different 32 bits that make up the four bytes.

Using this convention, we see that the largest value for the float type is when c = (11111111) 2 = (255) 10. This value for c yields an exponent of 2^255 −^127 = 2^128 ≈

  1. So the largest real number that can be expressed in the float type is around
  2. Similarly, the most negative real number that can be expressed is around − 1038. Between these two limits, there are only 2^31 − 1 ≈ 2 × 109 possible float numbers.

The smallest non-zero number is also determined by the exponent c. If c = 0, then the exponent is 2−^127 ≈ 6 × 10 −^39. Although the smalest float type number is 10−^39 , this doesn’t mean that we have precision to 39 significant figures. The number of significant figures is determined by the 23 bits in the mantissa (f) region. The number of digits after the decimal point

Note that the number representated by these bits for float is not exactly equal to 1 /10:

You shouldn’t get upset that the computer can’t store 1/10 exactly. It doesn’t have 10 fingers and it only has a finite amount of storage space for its zero’s and one’s.

Base 2 is probably a more universal base to use. If we were to communicate with intelligent life on other planets, most likely they will not be using base 10. Although we have 10 fingers and toes, we have two eyes, two ears, two hands, two arms, two feet, two legs, etc. Actually, base 8 would have been better than base 10. If we had chosen base 8, then in High School 7/8 = 87.5% would have been an ”A” grade, and we would have a special birthday every 8 years.