Declaring 64-bit variables in C

As you supposed, 1 is a plain signed int (which probably on your platform is 32 bit wide in 2’s complement arithmetic), and so is 43, so by any chance 1<<43 results in an overflow: in facts, if both arguments are of type int operator rules dictate that the result will be an int as … Read more

Have you ever had to use bit shifting in real projects? [closed]

I still write code for systems that do not have floating point support in hardware. In these systems you need bit-shifting for nearly all your arithmetic. Also you need shifts to generate hashes. Polynomial arithmetic (CRC, Reed-Solomon Codes are the mainstream applications) or uses shifts as well. However, shifts are just used because they are … Read more

Unsigned Right Shift / Zero-fill Right Shift / >>> in PHP (Java/JavaScript equivalent)

After looking into the two functions from the question (“shr9” and “shr11”) and merging/tweaking the good parts, I finally found the solution. All tests passed (I even added more in the demo), and it also works for shifts by a negative number. [Live Demo] function unsignedRightShift($a, $b) { if ($b >= 32 || $b < … Read more

Is BitArray faster in C# for getting a bit value than a simple conjuction with bitwise shift?

@Jonathon Reinhart, your benchmark is unfortunately inconclusive. It does not take into account the effects of possible lazy-loading, caching and/or prefetching (by the CPU, the host OS and/or the .NET runtime). Shuffle the order of the tests (or call the test methods multiple times) and you might notice different time measurments. I did your original … Read more

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