C left shift on 64 bits fail

Because 1 is an int, 32 bits, so (1 << 27)*27 overflows. Use 1ull. Regarding your comment, if x is a uint64_t, then 1 << x is still an int, but for the multiplication it would be cast to uint64_t, so there’d be no overflow. However, if x >= 31, 1 << x would be … Read more

Bitshift in javascript

In ECMAScript (Javascript) bitwise operations are always in 32-bit. Therefore 5799218898 is chopped into 32-bit which becomes 1504251602. This integer >> 13 gives 183624. In Python they are arbitrary-length integers. So there’s no problem. (And the numbers in Windows calculator are 64-bit, enough to fit 5799218898.) (And the correct answer should be 707912.)

Java: right shift on negative number

Because in Java there are no unsigned datatypes, there are two types of right shifts: arithmetic shift >> and logical shift >>>. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html Arithmetic shift >> will keep the sign bit. Unsigned shift >>> will not keep the sign bit (thus filling 0s). (images from Wikipedia) By the way, both arithmetic left shift and logical … Read more