Javascript - convert integer to array of bits

convert to base 2:

var base2 = (yourNumber).toString(2);

access the characters (bits):

base2[0], base2[1], base2[3], etc...

var a = 22;
var b = [];

for (var i = 0; i < 5; i++)
  b[i] = (a >> i) & 1;

alert(b);

Assuming 5 bits (it seemed from your question), so 0 <= a < 32. If you like you can make the 5 larger, upto 32 (bitshifting in JavaScript works with 32 bit integer).


Short (ES6)

Shortest (32 chars) version which fill last bits by zero. I assume that n is your number, b is base (number of output bits):

[...Array(b)].map((x,i)=>n>>i&1)

let bits = (n,b=32) => [...Array(b)].map((x,i)=>(n>>i)&1);

let Bitarr = bits(22,8);

console.log(Bitarr[0]); // = 0
console.log(Bitarr[1]); // = 1
console.log(Bitarr[2]); // = 1
console.log(Bitarr[3]); // = 0
console.log(Bitarr[4]); // = 1

This should do

for(int i = 0; i < 32; ++i)
  Bitarr[i] = (my_int >> i) & 1;