Casting isn't portable?
I ran the following code below and got 2 different outputs depending on the architecture I ran it on.
On x86 I got 0xffffffff. Which is exactly what I'd expect. But on powerpc I got 0xff. Is casting really not portable? If so, what is the best way to deal with it?
char x = -1;
int y = x;
char's are signed on some archs and unsigned on others.
As an experiment try signed char and unsigned char on both archs.
I'm not sure what you're getting at with that suggestion. But I did try replacing the char with a short and correctly got 0xffffffff. So I guess that means it is unsigned on the powerpc arch I'm using.
Unadorned char can be signed or unsigned the standard does not mandate which. There are compiler switches to force the use of one type though.
Replacing the char with a short doesn't prove anything (shorts are always signed unless you specifically request an unsigned short).
I meant to try forcing an unsigned char:
and then forcing a signed char:
unsigned char x = -1;
and see the difference.
signed char x = -1;
Oh. I didn't even realize there was a signed keyword. I've never seen that used anywhere.