.net – better way to do a 4 bits circular shifting

bit-manipulationnet

is this the optimal way of doing a 4 bits circular shifting?

    n << 1 ^ 0x10 | n >> 3

I was only testing with number that was actually "working"!

Best Answer

Did you actually try this? It is non-optimal, it generates garbage results. This ought to work better:

    static int RollLeft4Bits(int n) {
        return ((n << 1) & 15) | ((n >> 3) & 1);
    }