Jim corbat Jim corbat - 25 days ago 6
C Question

High order bit in C

I know that the difference between signed and unsigned integers is in the way the high-order bit of the integer is interpreted.

What exactly does high-order bit or low-order bit of the integer mean?

When there is specified signed integer then C compiler generate code that assumes the high-order bit is used as a sign flag.

How compiler use high order bit as a sign flag?

How does it look like in memory in binary format when I write

signed int num = 23;
?

Thank you.

Answer

What is exactly high-order bit or low-order bit of the integer means?

When you write down bits of a binary representation of a number left-to-right, including leading zeros (if any) the leftmost bit is the high-order bit; the rightmost bit is the low-order bit.

when there is specified signed integer then C compiler generate code that assumes the high-order bit is used as a sign flag? How compiler use high order bit as a sign flag?

This is specific to the hardware for which the compiler is written. The reason for it is that representation of negative numbers goes hand-in-hand with the arithmetic unit of the CPU, which produces a representation of negatives specific to the given hardware.

How it looks like in memory when I write signed int num = 23;

Since the number is 23, its signed representation in memory is the same as that of an unsigned representation of 23, i.e. 0x00000017 in hex on computers with 4-byte integers.

Comments