Transcript Document

Basic Data Types
There are essentially only two data Types:
• Integers and real (floating point) numbers
The number of BITS/Bytes required (for each type) can vary:
• Integers: 8 bits (1 byte), 16 bits (2 bytes), 32 bits (4 bytes)
• 32 bits (4 bytes) or 64 bits (8 bytes)
Let’s start with integers (the simplest basic data type)
• Data type char (8 bits) or data type char16_t* (16 bits)
Storing the integer 52 as the data type char (8 bits)
• 5210 = 1101002
0 0 1 1 0 1 0 0
Given 8 bits we have 28 = 256 possible combinations
Integers have two components
• The sign (negative or non-negative) and the value
• By default all integers are signed
0 0 1 1 0 1 0 0
Sign bit
Value
Binary conversion
Basic Data Types
With unsigned variables, we assume that all of the values will be => 0
With signed variables (default), we assume that you want to store +/- variables
• 1 bit will be used for the sign, so you have N-1 bits left to the values
Data Type
unsigned char
unsigned short
unsigned int
unsigned long
signed char (char)
signed short (short)
signed int (int)
signed long (long)
Bits
8
16
32
32
8
16
32
32
Range
0 to 28 -1
0 to 216 -1
0 to 232 -1
0 to 232 -1
-27 to 27 -1
-215 to 215 -1
-231 to 231 -1
-231 to 231 -1
Values
0 to 255
0 to 65,535
0 to 4,294,967,295
0 to 4,294,967,295
-128 to 127
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
-2,147,483,648 to 2,147,483,647
Basic Data Types
Consider (do more than that – enter the code) below
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
void main()
{
char mychar = 52;
unsigned char mychar2;
short sint;
int rint;
long lint;
printf("My characters are stored on %d and %d bytes\n", sizeof(mychar), sizeof (mychar2));
cout << "Variable mychar contains the value " << mychar << endl;
cout << "My short variable requires " << sizeof(sint) << " bytes of storage" << endl;
printf("My ints are stored on %d bytes\n", sizeof(rint));
printf("My Long Integers are stored on %ld bytes\n", sizeof(lint));
return 0;
}
As usual, compile and run
Basic Data Types
Assuming all went well, the output should look like
Basic Data Types
Real numbers (data type float) are different.
• All real numbers are signed (+/-)
• However, there is a 3rd component we have to keep track of.
Consider the following real numbers
-543.6 and -0.05436
Both are negative and the sequence of individual digits are the same
If we normalize (by putting the decimal point before the first significant digit)
and stating the value in scientific notation, we can note the similarities (and the
differences):
-0.5436 * 103 and -0.5436 * 10-1
The only difference is the (characteristic of) the exponent
One is 3 and the other -1
Basic Data Types
Thus floats capture:
• The sign (+/-): 1 bit
• The characteristic of the exponent (how many decimal paces the decimal point
is moved and in which direction)
• The normalized mantissa
“the part of a floating-point number that represents the significant digits of that
number, and that is multiplied by the base raised to the exponent to give the
actual value of the number”. *
Single precision floats generally are stored on 32 bits (4 bytes). We know the sign
requires 1 bit, which leaves 31 bits to distribute between the characteristic of the
exponent and the mantissa.
Which is preferred?? That depends on what you want (compilers vary)
Suppose that you allocate 12 bits to characteristic (and 19 to the mantissa)
212 bits = 4,096 implies that the range of characteristics is
-2,048 to 2,047 decimal places
Which means that you can get very small or very large numbers: MAGNITUDE
* www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=def%20mantissa
Basic Data Types
Having large magnitude lets you capture very small values (arsenic per gallon) or
very large values (atoms per square centimeter)
However, this is a zero-sum game: if you allocate 12 bits to the characteristic of
the exponent, you can only allocate 19 bits to the mantissa (PRECISION)
219 bits = 524,287 implies that you can represent ALL values to 5 decimal points
(you can’t represent the value 524,288) and some to six (anything less than
524,288))
Is that important? It is if you want to accurately display the value of pi!
So, what’s the solution??
As we said, it depends. The most common distribution is:
The mantissa (24 bits)
The characteristic of the exponent (7 bits)
The sign (+/-): 1 bit
Basic Data Types
Given this configuration, we have:
• Magnitude: 26 = 64 implying a range of -64 to 63 (the decimal point can move
64 places to the right or 63 places to the right)
• Mantissa: 224 = 16,777,216 implying that we have 7 decimal points of precision
(some values, such as this one can be represented to 8 decimal points of
precision)
As an example, suppose the mantissa is 0.8746896. The representations could be:
0.00000000000000000000000000000000000000000000000000008746896
OR
874,689,600,000,000,000,000,000,000,000,000,000,000,000,000,00,000,00,000,00,000,000
BUT
Only to seven decimal points of precision
The actual value of our latter value might be:
874,689,610,162,714,089,009,123,791,012,870,060,178,210,011,727,884,091,761,250,964
Basic Data Types
C/C++ has three types of real numbers:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
float f;
// also referred to as “single precision”
double d;
// also referred to as “double precision”
long double ld;
// Precision not less than double
printf("float bytes %d double bytes %d long double %d\n", sizeof(f), sizeof(d), sizeof(ld));
return 0;
}
Try entering, building and running the code
You should have gotten
Basic Data Types
What is the range of floating point numbers?
It really doesn’t make sense to talk about the range (as it does with integers)
For example, how many numbers lie between the number 1.01 and 1.02?
Floats are measure in terms of their Level of Precision (hence the terms single
precision and double precision)
Basic Data Types
Are there any additional data types?
Yes and No -•
You could make an argument that the data type (which stores the address (not the
contents stored at that address), or perhaps a URL address (hyperlink) or perhaps a
BLOB (a Binary Large OBject -- such as an image or sound file) are different, but in
reality they are typically an address which tells you were they are found.
There are, however, abstract data types, which are data types which you (the
programmer) create using the basic data types we just talked about
•
•
•
•
strings
arrays
structs (records)
stacks, queues, trees, list
We will talk about some of these later
Basic Data Types