c++ - Why doesn't left bit-shift, "<<", for 32-bit integers work as expected when used more than 32 times? -
when write following program , use gnu c++ compiler, output 1
think due rotation operation performed compiler.
#include <iostream> int main() { int = 1; std::cout << (a << 32) << std::endl; return 0; }
but logically, it's said bits lost if overflow bit width, output should 0. happening?
the code on ideone, http://ideone.com/vptwj.
this caused due combination of undefined behaviour in c , fact code generated ia-32 processors has 5 bit mask applied on shift count. means on ia-32 processors, range of shift count 0-31 only. 1
from the c programming language 2
the result undefined if right operand negative, or greater or equal number of bits in left expression’s type.
from ia-32 intel architecture software developer’s manual 3
the 8086 not mask shift count. however, other ia-32 processors (starting intel 286 processor) mask shift count 5 bits, resulting in maximum count of 31. masking done in operating modes (including virtual-8086 mode) reduce maximum execution time of instructions.
1 http://codeyarns.com/2004/12/20/c-shift-operator-mayhem/
2 a7.8 shift operators, appendix a. reference manual, c programming language
3 sal/sar/shl/shr – shift, chapter 4. instruction set reference, ia-32 intel architecture software developer’s manual
Comments
Post a Comment