Sign extension
Pascal Cuoq - 25th Apr 2013There is no “sign extension” in C. Please stop referring to “sign extension” in C programs.
In assembly, there is such a thing as “sign extension”
Sign-extend is an assembly instruction say movsx %al %ebx
to transfer the contents of a narrow register say %al
to a wide register say %ebx
copying the most significant bit of the narrow register all over the unoccupied bits of the wide register.
In C there is no “sign extension”. Really.
In C a short
variable s
contains -3
and the contents of that variable is transferred to another variable of type int
like so:
int i = s;
Variable i
receives -3
. That's it. That's “sign extension”.
It's an assignment and in this particular case a promotion. With other types it could have been a conversion. The target wide variable receives the value that was contained in the narrow variable. Why would this operation need a name and such an ugly name as “sign extension” at that?
The name for “sign extension” in C is “getting the value”. Variable i
receives the value of s
. That's it. That is all there is.