Why use the Bitwise-Shift operator for values in a C enum definition?

Maybe writing the values in hexadecimal (or binary) helps 🙂 enum { kCGDisplayBeginConfigurationFlag = (1 << 0), /* 0b0000000000000001 */ kCGDisplayMovedFlag = (1 << 1), /* 0b0000000000000010 */ kCGDisplaySetMainFlag = (1 << 2), /* 0b0000000000000100 */ kCGDisplaySetModeFlag = (1 << 3), /* 0b0000000000001000 */ kCGDisplayAddFlag = (1 << 4), /* 0b0000000000010000 */ kCGDisplayRemoveFlag = (1 … Read more

Why was 1

The relevant issue is CWG 1457, where the justification is that the change allows 1 << 31 to be used in constant expressions: The current wording of 5.8 [expr.shift] paragraph 2 makes it undefined behavior to create the most-negative integer of a given type by left-shifting a (signed) 1 into the sign bit, even though … 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.)