c - Compiler dependency in right shift operator on signed types: -
in following code,
#include <stdio.h> #include<conio.h> main() { int y=-01; y<<=3; printf("%d\n",y); y>>=3; printf("%d",y); getch(); }
i read in book while using right shift operator on y, signed bit may or may not preserved depending on compiler. why that? concept behind this? please elaborate.
some processors have "unsigned" right shift (fills sign bit 0's) or signed right shift (fills size bit copies of current sign bit). since processors have 1 or other, not both, standard doesn't try mandate 1 behavior or other.
for it's worth, many (most?) current processors have instructions both. example, x86 includes both shr
(logical shift right -- fills 0's) , sar
(arithmetic shift right -- fills copies of sign bit).
Comments
Post a Comment