Reputation: 20140
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"!
Upvotes: 0
Views: 113
Reputation: 942099
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);
}
Upvotes: 2
Reputation: 539
You could also generate a lookup table of every possible value and the shifted result. Then just use the input as an index into the lookup table to get the desired result
Upvotes: 1