Replace bit position:

xxxxx 0 xxxxxx 1 xxxxx -> xxxxx 1 xxxxxx 0 xxxxx

   i        j               i       j

Case 1: bit[i] == bit[j]: do nothing

Case 2: bit[i] != bit[j]: flip the two bits

int right_bit = ((x >>> i) & 1);
int left_bit = ((x >>> j) & 1);
// if the i-th bit and j-th bit are different
if (right_bit ^ left_bit == 1) {
    x ^= ((1 << i) | (1 <<j));
}

results matching ""

    No results matching ""