Signed and unsigned, and how bit extension works in C

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

How does the extension work? Added 2 bytes of a larger order, but I'm confused by the fact that there is 1 or 0. Probably it depends on the platform, so let's focus on what Unix does. Will two large bytes of order int be filled with 1 or 0, and why?

Basically, does the computer know what sis unsigned and correctly assign 0 higher order bits to int? So inow 0x0000ffff? Or, since it intsis signed by unix by default, is it necessary to use a bit-bit from s(a 1) and copy it to higher-order bytes?

+4
source share
1 answer

, unsigned . .

, , - , .

, C ( C99) ++ , , 10.

+10

Source: https://habr.com/ru/post/1742625/


All Articles