previousPowerOfTwo

Computes the previous power-of-two integer less than or equal to the provided non-negative integer. The maximum allowed input is (2^32)-1 due to 32-bit bitwise operator limitation in Javascript.

const n = Math.previousPowerOfTwo(29); // 16
const m = Math.previousPowerOfTwo(32); // 32

Return

The previous power-of-two integer.

Parameters

n

The integer to test in the range 0, (2^32)-1.

See also