previous | start | next

Signed Integer Conversions I

Signed Integer types are: char, short, int, long.

On many machines including our server, int and long are the same size: 4 bytes.

However, sizeof(char) = 1, sizeof(short) = 2, and sizeof(int) = 4 on our machine.

Smaller sizes can be assigned (converted) to larger sized integers by sign extension.

For example,

int x1;
int x2;
char c1 = 0x41; // decimal:   65; binary 0100 0001; so sign bit is 0
char c2 = 0x81; // decimal: -127; binary 1000 0001; so sign bit is 1
 
x1 = c1;        // binary: 0000 0000 0000 0000 0000 0000 0100 0001
                //    hex: 0x00000041; decimal: (still) 65

x2 = c2;        // binary: 1111 1111 1111 1111 1111 1111 1000 0001
                //    hex: 0xFFFFFF81; decimal (still) -127
   


previous | start | next