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

XSLT Bitwise Logic

XSLT is Turing-complete, see for example here or here, hence it can be done. But I have used XSLT only one or two times and can give no solution. UPDATE I just read a tutorial again and found a solution using the following fact. bitset(x, n) returns true, if the n-th bit of x is … Read more

How can I convert bits to bytes?

The code is treating the first bit as the low bit of the word, so you end up with each word reversed. As a quick-and-dirty fix, try this: bytes[byteIndex] |= (byte)(1 << (7-bitIndex)); That puts the first bit in the array at the highest position in the first byte, etc.

Bitwise shifting array of char’s

You have to shift and compare elementwise. for(i = 0; i < len; ++i) array[i] >>= 3; for example. If you want to move the bits shifted out of one element to the next, it’s more complicated, say you’re shifting right, then unsigned char bits1 = 0, bits2 = 0; for(i = len-1; i >= … Read more