|
|
|
|
|
by minor3rd
3079 days ago
|
|
I haven't used bitwise operators since college (approaching 8 years ago) but wanted to try this as a fun exercise during the NFL playoff game. Here's what I came up with -- mostly untested. [edit] Note this is obviously for 16-bit integers. int flip_endian_helper(int num, int i) { if (i == 16) {
return 0;
}
int shift = 15 - 2*i;
int workingBit = num & (1 << i);
int partial = shift > 0 ? workingBit << shift : workingBit >> (shift * (-1));
return partial | flip_endian(num, i + 1);
}int flip_endian(int num) { return flip_endian_helper(num, 0);
}
|
|