The square root of bitwise NOT, if it exists, would be some function *f* such that *f(f(x)) = not x*, or in other words, *f²(x) = not x*.
It is similar in concept to the √NOT gate in Quantum Computing, but in a different domain which makes the solution very different.

Before trying to find any specific *f*, it may be interesting to wonder what properties it would have to have (and lack).

*f*must be bijective, because its square is bijective.*f²*is an involution but*f*cannot be an involution, because its square would then be the identity.*f*viewed as a permutation (which can be done, because it has to be bijective) must be a derangement, if it had any fixed point then that would also be a fixed point in*f²*and the*not*function does not have a fixed point.

## Does *f* exist?

In general, a permutation has a square root if and only if the number of cycles of same even length is even. The *not*function, being an involution, can only consist of swaps and fixed points, and we already knew it has no fixed points so it must consist of only swaps. A swap is a cycle of length 2, so an even length. Since the

*not*function operates on

*k*bits, the size of its domain is a power of two,

*2*. That almost always guarantees an even number of swaps, except when

^{k}*k = 1*. So, the

*not*function on a single bit has no square root, but for more than 1 bit there are solutions.

*f* for even k

For 2 bits, the *not* function is the permutation (0 3) (1 2). An even number of even-length cycles, as predicted. The square root can be found by interleaving the cycles, giving (0 1 3 2) or (1 0 2 3). In bits, the first looks like:

in | out |

00 | 01 |

01 | 11 |

10 | 00 |

11 | 10 |

Which corresponds to swapping the bits and then inverting the lsb, the other variant corresponds to inverting the lsb first and then swapping the bits.

That solution can be applied directly to other even numbers of bits, swapping the even and odd bits and then inverting the even bits, but the square root is not unique and there are multiple variants. The solution can be generalized a bit, combining a step that inverts half of the bits with a permutation that brings each half of the bits into the positions that are inverted when it is applied twice, so that half the bits are inverted the first time and the *other* half of the bits are inverted the second time. For example for 32 bits, there is a nice solution in x86 assembly:

bswap eax xor eax, 0xFFFF

*f* for odd k

Odd k makes things less easy. Consider k=3, so (0 7) (1 6) (2 5) (3 4). There are different ways to pair up and interleave the cycles, leading to several distinct square roots:

- (0 1 7 6) (2 3 5 4)
- (0 2 7 5) (1 3 6 4)
- (0 3 7 4) (1 2 6 5)
- etc..

in | 1 | 2 | 3 |

000 | 001 | 010 | 011 |

001 | 111 | 011 | 010 |

010 | 011 | 111 | 110 |

011 | 101 | 110 | 111 |

100 | 010 | 001 | 000 |

101 | 100 | 000 | 001 |

110 | 000 | 100 | 101 |

111 | 110 | 101 | 100 |

These correspond to slightly tricky functions, for example the first one has as its three from lsb to msb: the msb but inverted, the parity of the input, and finally the lsb. The other ones also incorporate the parity of the input in some way.