# awesome-bits A curated list of awesome bitwise operations and tricks

Maintainer - Keon Kim
Please feel free to pull requests

## Integers

Set nth bit

Unset nth bit

Toggle nth bit

Round up to the next power of two

Get the maximum integer

Get the minimum integer

Get the maximum long

Multiply by 2

Divide by 2

Multiply by the mth power of 2

Divide by the mth power of 2

Check Equality

This is 35% faster in Javascript

Check if a number is odd

Exchange (swap) two values

Get the absolute value

Get the max of two values

Get the min of two values

Check whether both numbers have the same sign

Flip the sign

Calculate 2n

Whether a number is power of 2

Modulo 2n against m

Get the average

Get the mth bit of n (from low to high)

Set the mth bit of n to 0 (from low to high)

Check if nth bit is set

Isolate (extract) the right-most 1 bit

Isolate (extract) the right-most 0 bit

Set the right-most 0 bit to 1

Set the right-most 1 bit to 0

n + 1

n - 1

Get the negative value of a number

`if (x == a) x = b; if (x == b) x = a;`

Different rightmost bit of numbers m & n

Common rightmost bit of numbers m & n

## Floats

These are techniques inspired by the fast inverse square root method. Most of these
are original.

Turn a float into a bit-array (unsigned uint32_t)

Caveat: Type pruning via unions is undefined in C++; use `std::memcpy` instead.

Turn a bit-array back into a float

Approximate the bit-array of a positive float using `frexp`

`frexp` gives the 2n decomposition of a number, so that `man, exp = frexp(x)` means that man * 2exp = x and 0.5 <= man < 1.

Caveat: This will have at most 2-16 relative error, since man + 125 clobbers the last 8 bits, saving the first 16 bits of your mantissa.

Fast Inverse Square Root

Caveat: We’re using the `i2f` and the `f2i` functions from above instead.

See this Wikipedia article for reference.

Fast nth Root of positive numbers via Infinite Series

See this blog post regarding the derivation.

Fast Arbitrary Power

Caveat: The `0x5c416` bias is given to center the method. If you plug in exp = -0.5, this gives the `0x5f3759df` magic constant of the fast inverse root method.

See these set of slides for a derivation of this method.

Fast Geometric Mean

The geometric mean of a set of `n` numbers is the nth root of their
product.

See here for its derivation.

Fast Natural Logarithm

Caveat: The bias term of `0x66774` is meant to center the method. We multiply by `ln(2)` at the end because the rest of the method computes the `log2(x)` function.

See here for its derivation.

Fast Natural Exp

Caveat: The bias term of `0x38aa22` here corresponds to a multiplicative scaling of the base. In particular, it
corresponds to `z` such that 2z = e

See here for its derivation.

## Strings

Convert letter to lowercase:

Convert letter to uppercase:

Invert letter’s case:

Letter’s position in alphabet:

Get letter’s position in alphabet (for Uppercase letters only):

Get letter’s position in alphabet (for lowercase letters only):

## Miscellaneous

Fast color conversion from R5G5B5 to R8G8B8 pixel format using shifts

Note: using anything other than the English letters will produce garbage results