--- layout: post author: Pascal Cuoq date: 2013-04-25 20:14 +0200 categories: conversions-and-promotions rant format: xhtml title: "Sign extension" summary: --- {% raw %}
There is no “sign extension” in C. Please stop referring to “sign extension” in C programs.
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 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.