C++ Basic: bits, simply data type
Jin Tu
Bolg: http://www.inblogs.net/havdone
Abstraction
This article help to make sense of the various data types in programming languages by C++ data type.
32 bits Operation System
X-bits (32 bits or 64bits) operation system refers primarily that the number of bits used to represent memory addresses is x bits in operation system. (www.webopedia.com)
In 32 bits windows, the length of all pointers is 32 bits. “Operating systems based on Microsoft Windows NT technologies have always provided applications with a flat 32-bit virtual address space that describes 4 gigabytes (GB) of virtual memory. The address space is usually split so that 2 GB of address space is directly accessible to the application and the other 2 GB is only accessible to the Windows executive software.”(Microsoft)
C++ simply data type
The simply data type is the fundamental data type in c++ as it has become the building block of a structured data type. (Malik, D, S 2004) It can be categorized into three types:
1. Intergral: to deal with integers, or numbers without a decimal part;
2. Floating-point: to deals with decimal numbers;
3. Enumeration type: it is a user-defined data type. (Malik, D, S 2004)
Integral data type in C++ contains:
l short(2 bytes)
l long(4 bytes)
l int(4 bytes)
l bool(1 byte)
l char(1 bytes)
l unsigned char
l unsigned short
l unsigned int
l unsigned long
The smallest data type is char (1 byte), and the largest data type is long. And 1 byte equals 8 bits.
Floating-point data type contains:
l float(4 bytes)
l double(8 bytes)
l long double (8 bytes)
² Long double and double actually is the same for the newer compiler;
² A wchar_t data type actually is an unsigned short type, as they are all unsigned double bytes;
² A bool type also is a int type, and its value is zero or one;
² A int type is a long type
The differences between long and float
float i=111111;//111111.00
long l=111111;//111111
float data type:80 03 d9 47
long data type:07 b2 01 00
As mentioned in the above section, long is a data type which deals with integer and numbers without decimal part, and float is a data type which deals with decimal number(-3.4E+38 to 3.4E+38).
Type Conversion
Implicit or explicit conversion
It is likely that successfully implicit or explicit converting smaller bytes of data type to larger bytes of data type, otherwise, it is likely that uncertain danger occur. The positively safe conversions include:
l int to float and float to double
l char to wchar_t
l char to int
l short to long
l bool to int
l wchar_t to unsigned short
l long to int and short to int
l
It should be verified by programmers whether it is safe for conversions except the above. For instance:
l int to char or int to long or int to short
l wchar_t to char
l short to wchar_t
Conversion functions
Example
//////////////////////////////////////////////////////////
//Pretend 1 byte = 8 bits
//////////////////////////////////////////////////////////
int i=9;// 4 bytes=32bits
short s=1;//2 bytes
long l=2L;//4 bytes
char c='3';//1byte
float f=111111;//4 bytes
long l1=111111;
double d=333333;//8 bytes
long double ld=333333;// 8 bytes
wchar_t w='4';//2 bytes
wchar_t wc=L'五';//2 bytes, L get the Unicode of right nearest string or char,
//make targets to 2 bytes unsigned data type
unsigned uVal = 328u; // Unsigned value
long lVal = 0x7FFFFFL; // Long value specified
//as hex constant
unsigned long ulVal = 0776745ul; // Unsigned long value
////////////////////////////////////////////////////////////////////////
//Convertion
//from smaller bytes of data type to larger bytes of data type, it is safe!
//from a data type to the same bytes of data type, it is safe!
//////////////////////////////////////////////////////////////////////
//char to int: 1 byte to 4 byte
char cc='h';
int ic=int(cc);//equal static_cast<int> cc
//put pointer's value (address) into long data type.
//pointer is 32-bit(4 bytes) data type in 32 bit OS
//4 bytes to 4 bytes
char * pc="test";
long sc =long(pc);
long * lp=(long*)sc;
//char to wchar_t:1 byte to 2 byte
wchar_t wcc=wchar_t(cc);
//wchar_t to short: 2 bytes to 2 bytes
short sct=short(wcc);
//wchar_t * to short *:ponter of 2bytes to pointer of 2bytes
wchar_t* wcp=L"fsd";
long lwcp=long(wcp);
unsigned short * uswcp=(unsigned short*)lwcp;
unsigned short * sp=(unsigned short *) wcp;
来源:https://www.cnblogs.com/havdone/archive/2008/01/11/1035829.html