Unexpected C/C++ bitwise shift operators outcome

后端 未结 6 1834
攒了一身酷
攒了一身酷 2020-11-28 16:02

I think I\'m going insane with this.

I have a a piece of code that needs to create an (unsigned) integer with N consequent bits set to 1. To be exact I

6条回答
  •  执念已碎
    2020-11-28 16:46

    Shifting by as many or more bits than in the integer type you're shifting is undefined in C and C++. On x86 and x86_64, the shift amount of the shift instructions is indeed treated modulo 32 (or whatever the operand size is). You however cannot rely on this modulo behaviour to be generated by your compiler from C or C++ >>/<< operations unless your compiler explicitly guarantees it in its documentation.

提交回复
热议问题