mixing signed and unsigned

T

T

Given:

typedef unsigned short u16;
typedef signed int i32;

u16 u0 = 0xFFFF;
i32 i1 = u0;

u16 u1 = 0;
i32 i2 = u1 - (u16)1;

printf("p1: %d, %d \n", i1, i2 );
-------------------------------------------------------

Why do i1 & i2 have different results?

Isn't the rhs of i2 supposed to be done entirely in u16?

or put another way, why isn't the above i2 computed like this
i2 = u16( u1 - (u16)1)
 
A

Alf P. Steinbach

* T:
Given:

typedef unsigned short u16;
typedef signed int i32;

u16 u0 = 0xFFFF;
i32 i1 = u0;

u16 u1 = 0;
i32 i2 = u1 - (u16)1;

printf("p1: %d, %d \n", i1, i2 );
-------------------------------------------------------

Why do i1 & i2 have different results?

Isn't the rhs of i2 supposed to be done entirely in u16?

No. It's done in 'int'. Integer promotions.
 
A

Alf P. Steinbach

* T:
then why isn't u0 promoted in the same manner?

I'd love to answer this, but I have no idea what you're talking about
since you didn't quote anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top