1)unsigned short is converted into int,if short is smaller than int.please give an example of this situation.

2)if two are of the same size,then unsigned short is converted into unsigned int.please give an example of the situation.

2)if two are of the same size,then unsigned short is converted into unsigned int.please give an example of the situation.

1)

unsigned short x = 0;

int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

2)

unsigned short x = 0;

unsigned int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

In the both cases x is converted to the type of y becuase the rank of y is higher than rank of x.

I will give a more interesting example if you deal with 32-bit platform. In such platform sizeof( int ) and correspondingly sizeof( unsigned int ) is equal to sizeof( long ) which in turn equal to 4. Now the example

long x = 0;

unsigned int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

The common type for x and y will be**unsigned long**.

unsigned short x = 0;

int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

2)

unsigned short x = 0;

unsigned int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

In the both cases x is converted to the type of y becuase the rank of y is higher than rank of x.

I will give a more interesting example if you deal with 32-bit platform. In such platform sizeof( int ) and correspondingly sizeof( unsigned int ) is equal to sizeof( long ) which in turn equal to 4. Now the example

long x = 0;

unsigned int y = 0;

std::cout << typeid( x + y ).name() << std::endl;

The common type for x and y will be

Last edited on

Topic archived. No new replies allowed.