tags:

views:

96

answers:

2
unsigned short s;
s = 0xffff;
int i = s;

How does the extension work here? 2 larger order bytes are added, but I'm confused whether 1's or 0's are extended there. This is probably platform dependent so let's focus on what Unix does. Would the two bigger order bytes of the int be filled with 1's or 0's, and why?

Basically, does the computer know that s is unsigned, and correctly assign 0's to the higher order bits of the int? So i is now 0x0000ffff? Or since ints are default signed in unix does it take the signed bit from s (a 1) and copy that to the higher order bytes?

+5  A: 

No, an unsigned value is never sign-extended. Upcasting will always pad such a value with zeroes.

More precisely, the unsigned variable represents a particular number, and it will still represent the same number after a cast, provided that is possible in the new format.

Hexadecimal or no, C (although not C99) and C++ are designed to work in the absence of bits, eg with base-10 numerics.

Potatoswatter
+1 Conceptually an unsigned number can't be sign extended. It doesn't have a sign bit to extend from. (Edit: although C and C++ are tied to a binary representation of integers so I don't really buy your last paragraph.)
Charles Bailey
I would have phrased that last sentence as "the C standards have always left such holes of undefinedness, partly in order to accommodate pre-existing implementations, that you could have a standard-conforming compiler that does not use bits".
Pascal Cuoq
@Pascal: If C++ is any indication, adding certainty to base-10 support (`numeric_limits<>::radix`) is the trend.
Potatoswatter
so c 'knows' that my variable s here is unsigned, and therefore pads with 0's to keep it the same number?
hatorade
@hator: it knows since you declared it that way…
Potatoswatter
@Pascal: But it must act as though it uses bits. _6.2.6.2 Integer types_ (under 6.2.6 Representation of types) describes everything in terms of bits and powers of two.
Charles Bailey
@Charles: I didn't realize C99 was so restrictive. Well, in any case, I believe that BCD was a big deal before C99, so reasonable to say C *was* designed to work that way, and C++ isn't adding any such restrictions.
Potatoswatter
@Charles Indeed, even the existence of exactly one sign bit for signed quantities is explicitly mentioned. Thanks for drawing my attention to this.
Pascal Cuoq
It's the same (basically) for C++. I don't think that `numeric_limits<>::radix` is ever likely to return anything other than two for built-in integer types.
Charles Bailey
@Charles C++ unsigned numbers are supposed to be base-2… but it's all academic as wacky machines just don't like playing by the rules.
Potatoswatter
+1  A: 

Since i is int, higher order bits will be filled with 1's, because it will be two's complement representation. Actually, would be processor dependent, not platform.

EDIT: oopsie, I'm completely wrong here, see the other answer, Potatoswatter, for the correct info.

Chris O