Signed and unsigned, and how bit extension works in C

Posted by hatorade on Stack Overflow See other posts from Stack Overflow or by hatorade
Published on 2010-04-24T23:34:28Z Indexed on 2010/04/24 23:43 UTC
Read the original article Hit count: 148

Filed under:
|
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?

© Stack Overflow or respective owner

Related posts about c

    Related posts about datatypes