# bit hacks

Test if the n^{th} bit is set.

```
(x & (1 << n))
```

Set the n^{th} bit.

```
x = x | (1 << n)
```

Unset the n^{th} bit.

```
x = x & ~(1 << n)
```

Toggle the n^{th} bit.

```
x = x ^ (1 << n)
```

http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/