The answer as to what happens in a sign extension depends on the way that signed integers are represented. There are several ways that can be used to represent negative numbers in binary form. The most common variants are:
- Two's complement. By far the most common.
- One's complement. Sometimes used in DSP systems, I believe.
- Sign and magnitude.
- Excess K. Used in floating point exponents.
- Base -2.
There are most likely another few variants, but since number 1 in that list probably covers at least 99% of the computer systems the readers here are going to encounter, and the other four will be the vast majority of any remaining systems (in fact, I have only ever encountered Excess K in "real life", the others are in my 30+ years of working with computers, still theoretical). The last two forms are not allowed in the C99 standard, so it's unlikely [but it's not absolutely forbidden to write a C compiler for a target system that is non-conformant - you just can't claim that it supports the full C99 standard]
I will now explain how a sign extend will work in each of these number systems. I do NOT intend to explain how each system works in other respects, or why one would pick a particular system, where they are used, or something else like that. That probably belongs in a "computer architecture book". For further details on different number systems, there is an article in Wikipidea here.
I will give examples of how a two 4-bit numbers convert to their respective 8-bit versions - to save typing lots of ones and zeros, the principle is the same, just more digits.
Two's complement:
Take bit 31 (the sign bit) and copy it into the bits 32..63. Most 64-bit processors have a specific instruction to perform this step, so that the conversion from 32- to 64-bit is automatic.
The value 5 is 0101 as a four bit number, copy bit 3 -> 00000101. -6 in 4 bits is 1010, copy bit 3 -> 11111010.
One's complement:
Identical to two's complement - the only real difference is that there is a negative zero, formed by 64 "ones".
The value 5 is 0101 as a four bit number, copy bit 3 to bit 4..7-> 00000101. -6 in 4 bits is 1001, copy bit 3 to bit 4..7 -> 11111001.
Sign and magnitude
Move bit 31 into bit 63. The rest of the extended digits remain zero.
The value 5 is 0101 as a four bit number, copy bit 3 to bit 7 and replace with a zero -> 00000101. -6 in 4 bits is 1110, copy bit 3 to bit 7, and replace with zero -> 10000110.
Excess K
Since excess K is a "biased" representation, sign extension means normalizing and then re-biasing the number with the larger constant. In other words, subtract 231 and add 263 [or "add 263-231"].
The value 5 is 1101 as a four bit number, subtract 8, then add 128 -> 10000101. -6 in 4 bits is 0010, subtract 8, then add 128 -> 01111010.
(This is often used in the exponent of floating point numbers - I believe that, together with having the sign in the top-most bit, allows all "regular" floating point numbers to be compared as a 32- or 64-bit integer and still behave as expected - but do not use this in your programs!).
Base -2
This is a really weird one, as the value is -2value, which, if you follow the link above, you'll see is not at all trivial to follow. Odd numbered bits are negative, even numbered bits values are positive. However, the sign extension is trivial - just add zeros to the relevant level, since each bit is has a sign or not based on being even or odd.
The value 5 is 0101 as a four bit number, fill with zeros to extend: 00000101. -6 in 4 bits is 1110, fill with zeros gives 00001110.