// Set the nth bit to 1
x = (1 << n) | x
// Set the nth bit to 0
x = ~(1 << n) & x
// toggle the nth bit
x = (1 << n) ^ x
x = (x-1) | x
x = (~x) + 1
// ex : 0010 -> 1110 = 2 -> -2
Take advantage of the property that the negative and positive representation of a number in bits only share the least significant 1 bit.
For example 2 and -2, 0010 and 1110 share only the right most 1 is shared.
x &= -x
x &= (x-1)