Skip to content

prevPow2

Subhajit Sahu edited this page Feb 3, 2021 · 9 revisions

Gets previous [power-of-2]. [:running:] [:vhs:] [:package:] [:moon:] [:ledger:]

Similar: [log2], [pow2], [isPow2], [nextPow2], [prevPow2].


integer.prevPow2(x);
// x: an int32
const integer = require("extra-integer");

integer.prevPow2(63);
// 32

integer.prevPow2(128);
// 128

integer.prevPow2(1);
// 1

<br>
<br>


## References

- [Bit-shift method: Bit Twiddling Hacks](https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2)
- [bit-twiddle: @mikolalysenko](https://www.npmjs.com/package/bit-twiddle)

[power-of-2]: https://en.wikipedia.org/wiki/Power_of_two
[log2]: https://github.com/nodef/extra-integer/wiki/log2
[pow2]: https://github.com/nodef/extra-integer/wiki/pow2
[isPow2]: https://github.com/nodef/extra-integer/wiki/isPow2
[nextPow2]: https://github.com/nodef/extra-integer/wiki/nextPow2
[:running:]: https://npm.runkit.com/@extra-integer/prev-pow2
[:package:]: https://www.npmjs.com/package/@extra-integer/prev-pow2
[:moon:]: https://www.npmjs.com/package/@extra-integer/prev-pow2.min
[:ledger:]: https://unpkg.com/@extra-integer/prev-pow2/
[:vhs:]: https://asciinema.org/a/337158
[prevPow2]: https://github.com/nodef/extra-integer/wiki/prevPow2
Clone this wiki locally