Skip to content
Advertisement

Convert number to “boolean bit array” in JavaScript

I have a number 5. How to get a “boolean bit array” like the array below?

array = [false, false, true, false, true]; // 00101

(Need reserve the first two false)

Advertisement

Answer

This will do:

("0000" + Math.abs(number).toString(2)).slice(-5).split("").map(Number).map(Boolean)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement