비트 마스크를 가로로 뒤집을 수있는 쉬운 방법이 있습니까? 그것은 다음을 수행해야 더 우아한 방법이 있나요가로로 비트 마스크를 뒤집기
uint16 flipUint16Horizontally(uint16 bitmask)
{
uint16 flippedMask = 0;
for(unsigned int bit = 0; bit < 16; ++bit)
{
uint16 currentBit = (bitmask & (1 << bit)) >> bit;
flippedMask |= currentBit << (15 - bit);
}
return flippedMask;
}
:
0b1010101111001101 -> 0b1011001111010101
직선 앞으로 솔루션은 조금만 비트 일을 할 것인가?
우아함을 정의하십시오. – 2501
@ 2501 : 어리석은 방법이 아닙니다 .-) – m47h
여기에는 여러 가지 접근법이 있습니다 (https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious); 그러나, 아이디어의 아무도는 "one-liner"에게 불릴 수 없다. – Codor