bit manipulation - C - Swap a bit between two numbers -
i tried code:
void swapbit(unsigned char* numba, unsigned char* numbb, short bitposition)//bitposition 0-x { unsigned char oneshift = 1 << bitposition; unsigned char bita = *numba & oneshift; unsigned char bitb = *numbb & oneshift; if (bita) *numbb |= bita; else *numbb &= (~bita ^ oneshift); if (bitb) *numba |= bitb; else *numba &= (~bitb ^ oneshift); }
to swap bit position x of , b because of if() think there's better.
also when see this:
*numbb &= (~bita ^ oneshift);
i think there's easier way it. if have me, take :)
thanks in advance
first should set corresponding position in number 0
, , or actual bit, removing of conditions:
*numbb &= ~oneshift; // set bit `0` *numbb |= bita; // set actual bit value
the same other number.
Comments
Post a Comment