Is there any difference between type casting & type conversion in c++.
Type casting may do a minimum amount of conversion:
signed char Schar; // 1 byte (8 bits) to hold 256 values: -128 to 127
unsigned char Uchar; // 1 byte (8 bits) to hold 256 values: 0 to 255
...
if ( Schar < -10 ) ... // compiler uses SIGNED comparision
Uchar = Schar; // implicit conversion only copies the 8 bits
Uchar = (char) Schar; // explicit conversion may be required by compiler
if ( Uchar > 200 ) ... // compiler uses UNSIGNED comparision
...OR...
if ( (unsigned char) Schar > 200 ) ... // explicit conversion for UNSIGNED comparision
short Sshort; // 2 bytes (16 bits) to hold 65536 values: -32768 to 32767
unsigned short Ushort; // 2 bytes (16 bits) to hold 65536 values: 0 to 65536
...
// when moving 8 bits into 16 bit variables, what to do with other 8 bits ?
Sshort = (signed short) Uchar; // move 8 bits over and use 0s for other 8 bits
Sshort = (signed short) Schar; // same, but use 1s if negative to make Sshort negative
But this might be considered Type Conversion:
float dbl; // 4 bytes to store floating number in IEEE format
long lng; // 4 bytes to store 32 bit integer value in 2's complement format
...
dbl = lng; // convert from 2's comp to IEEE format - all bits change !
dbl = (float) lng; // explicit form
NOTE: int
is usually the same as short
or long
depending on compiler/CPU
NOTE: signed
is usually optional as this is often the default
No conversion takes place when you specify all variables occupy the same memory space:
typedef union MYUNION // all members occupy same space (memory bytes)
{
signed char Schar; // usual default for char
unsigned char Uchar;
signed short Sshort; // usual default for short
unsigned short Ushort;
signed long Slong; // usual default for long
unsigned long Ulong;
float flt;
double dbl;
};
MYUNION myunion;
myunion.Schar = ... // set variable (memory byte) to value
if ( (unsigned char) myunion.Schar > 200 ) ... // unsigned compare works ok
... is same as (also without moving any data around) ...
if ( myunion.Uchar > 200 ) ... // unsigned compare works ok
... myunion.Sshort ... // other 8 bits are UNASSIGNED GARBAGE !
myunion.Sshort = myunion.Schar; // provide all 16 bits from Schar
... myunion.Sshort ... // Sshort of valid now
myunion.dbl = 12345.0;
... myunion.Ulong ... // has weird value from odd IEEE bit format
myunion.Ulong = (unsigned long) myunion.dbl; // do explicit conversion
... myunion.Ulong ... // has CONVERTED 12345 value
NOTE: *(unsigned long*)&dbl
also produces weird values. It does:
a) take address (location of the bits and bytes) of double dbl
b) consider the address as an address of an unsigned long
c) get unsigned long from that location
Of course, there are some real applications of this technique. Examples: parsing a complicated external binary file or on CPUs with 512 bytes of memory, etc.