data representation
Data Representation
Most of us write numbers in Arabic form, ie, 1, 2, 3,..., 9. Some people write them differently, such as I, II, III, IV,..., IX. Nomatter what type of representation, most human beings can understand, at least the two types I mentioned. Unfortunately the computer doesn't. Computer is the most stupid thing you can ever encounter in your life.Modern computers are built up with transistors. Whenever an electric current pass into the transistors either an ON or OFF status will be established. Therefore the computer can only reconize two numbers, 0 for OFF, and 1 for ON, which can be referred to as BIT. There is nothing in between Bit 0 and Bit 1 (eg Bit 0.5 doesn't exist). Hence computers can be said to be discrete machines. The number system consists only of two numbers is called Binary System. And to distinguish the different numbering systems, the numbers human use, ie 1,2,3,4..., will be called Decimals (since they are based 10 numbers) from now on.
How, therefore, can computer understand numbers larger than 1? The answer is simple, 2 is simply 1+1, (like 10 = 9+1 for human) the numbers are added and overflow digit is carred over to the left position. So (decimal) 2 is representated in Binary as 10. To further illustrate the relationship, I have listed the numbers 1 to 9 in both systems for compaison:
Decimal | Binary |
---|---|
0 | 0000 0000 |
1 | 0000 0001 |
2 | 0000 0010 |
3 | 0000 0011 |
4 | 0000 0100 |
5 | 0000 0101 |
6 | 0000 0110 |
7 | 0000 0111 |
8 | 0000 1000 |
9 | 0000 1001 |
ASCII FORMAT
Not only does the computer not understand the (decimal) numbers you use, it doesn't even understand letters like "ABCDEFG...". The fact is, it doesn't care. Whatever letters you input into the computer, the computer just saves it there and delivers to you when you instruct it so. It saves these letters in the same Binary format as digits, in accordance to a pattern. In PC (including DOS, Windows 95/98/NT, and UNIX), the pattern is called ASCII (pronounced ask-ee) which stands for American Standard Code for Information Interchange.In this format, the letter "A" is represented by "0100 0001" ,or most often, referred to decimal 65 in the ASCII Table. The standard coding under ASCII is here. When performing comparison of characters, the computer actually looks up the associated ASCII codes and compare the ASCII values instead of the characters. Therefore the letter "B" which has ASCII value of 66 is greater than the letter "A" with ASCII value of 65.
Data Types
The computer stores data in different formats or types. The number 10 can be stored as numeric value as in "10 dollars" or as character as in the address "10 Main Street". So how can the computer tell? Once again the computer doesn't care, it is your responsibility to ensure that you get the correct data out of it. (For illustration character 10 and numeric 10 are represented by 0011-0001-0011-0000 and 0000-1010 respectively — you can see how different they are.) Different programming launguages have different data types, although the foundamental ones are usually very similar.C++ Basic Data Types (C++ specific)
C++ has many data types. The followings are some basic data types you will be facing in these chapters. Note that there are more complicated data types. You can even create your own data types. Some of these will be discussed later in the tutorial.Data Type |
Bytes
|
Data Range
| Remarks |
char |
1
|
ASCII -128 to127
| |
unsigned char |
1
|
ASCII 0 to 255
| including high ASCII chars |
int |
2
|
-32768 to 32767
| Integer |
unsigned (unsigned int) |
2
|
0 to 65535
| non-negative integer |
long int |
4
|
± 2 billions
| double sized integer |
unsigned long int |
4
|
0 to 4 billion
| non-negative long integer |
float |
4
|
3.4 ±e38
| 6 significant digits |
double |
8
|
1.7 ±e308
| 15 significant digits |
Comments
Post a Comment