"Problem: Given unsigned integer data, you need to flip its bits (flipped by the center image)"
There are a number of workarounds for this problem, and here are some tips for solving this problem using XOR (XOR) operations.
Tip: How do I exchange the bit I bits and the J bit data? Try to figure out how to do it with XOR or XOR arithmetic.
Different or arithmetic techniques:
To implement rollover we need to exchange the first N/2 of this data (assuming that the valid bits of the data are N, binary) and the post N/2 significant bits. Write a function swapbits (i, J), which is used to exchange the bits I and J bits.
Review the XOR operation:
0 0 0 1 1 0 0 1 1 1 0 1.
Exchange is required only if the I and J bits are different. Test the two bit whether the same is used for the XOR operation, if different, the two bits at the same time reversed on the line, here can also use the XOR operation. The code is as follows:
typedef unsignedint UINT;UINTSwapbits (UINTXUINTIUINTj) { UINTLo = ((x >> i) &1); UINTHi = ((x >> J) &1); if(Lo ^hi) {x^= ((1U<< i) | (1U<<j)); } returnx;} UINTReversexor (UINTx) { UINTn =sizeof(x) *8; for(UINTi =0; I < n/2; i++) {x= Swapbits (x, I, n-i-1); } returnx;}
Flipping bits of unsigned integer data