Find binary representation of x and store it in bin . get ( ] . bin . get ( 0 ] contains least significant bit ( LSB ) , next bit is in bin . get ( 1 ] , and so on .
ArrayList < Integer > bin = new ArrayList < Integer > ( ) ; while ( x != 0 ) { bin . add ( x & 1 ) ; x >>= 1 ; }