-->
Page 1 of 1

convert number to bits

PostPosted: Sun Jan 25, 2015 2:51 am
by robbaker
Hey,

I'm new to Lua and have been struggling...

does anyone know how to convert numbers to bit arrays Or binary strings?

For example :
123456 =11110001001000000

Thanks
Rob

Re: convert number to bits

PostPosted: Sun Jan 25, 2015 8:28 am
by Fr4gg0r
divide by 2 -> last bit
divide new number by 2 -> second last bit
and so on

Re: convert number to bits

PostPosted: Mon Jan 26, 2015 3:05 pm
by robbaker
Please let me know if there is a nicer way of doing this:

Code: Select allfunction toBits(x)
     bits = {}
     repeat
          table.insert (bits, 1, x%2)
          x = x/2
     until x==0
     return bits
end