22.04.2012, 19:16
The reason your function didn't work well is you're doing an arithmetic shift (>>) instead of a logical shift (>>>).
An arithmetic will preserve the leftmost bit and fill ones to the right of it, but the logical shift will simply shift bits to the right and put 0.
For example, 0xFF000000 >>> 8 yields 0x00FF0000, but 0xFF000000 >> 8 yields 0xFFFF0000.
An arithmetic will preserve the leftmost bit and fill ones to the right of it, but the logical shift will simply shift bits to the right and put 0.
For example, 0xFF000000 >>> 8 yields 0x00FF0000, but 0xFF000000 >> 8 yields 0xFFFF0000.