3 ms·
Bit tricks. Like public static int bitCount(int i) { i = i - ((i >>> 1) & 0x55555555); i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
by vbezhenar 7y ago
Bit tricks. Like
public static int bitCount(int i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
It was like magic for me when I encountered it first time.
- soylentgraham 7y agoCame here to say this, I first saw this in the book Hackers Delight which is full of 200X-me's mind blowing stuff. It really opened a door to writing (arb)GPU shaders where at the time if's weren't allowed and trying to cut branches from PS2/GameCube/Xbox code
- Tepix 7y agoCheck out the Othello implementation by Hans Wennborg with a bitboard at https://www.hanshq.net/othello.html https://www.hanshq.net/othello.html