why shift int a=1 to left 31 bits then to right 31 bits, it becomes -1
given int a = 1; ( 00000000000000000000000000000001 ), what I did is just a=(a<<31)>>31; I assume a should still be 1 after this statement (nothing changed I think). However, it turns out to be -1 ( 11111111111111111111111111111111 ). Anyone knows why? What you are missing is that in C++ right shift >> is implementation defined. It could either be logical or arithmetic shift for a signed value. In this case it's shifting in 1 s from the left to retain the sign of the shifted value. Typically you want to avoid doing shifts on signed values unless you know precisely that they will be positive or