On Nov 22, 2005, at 4:23 PM, Curt Hibbs wrote: > Does anyone have a clever way to convert an integer to an array of bit > values? > > For example (using 8 bit integers): > > 0 => [0, 0, 0, 0, 0, 0, 0, 0] > 1 => [0, 0, 0, 0, 0, 0, 0, 1] > 2 => [0, 0, 0, 0, 0, 0, 1, 0] > 7 => [0, 0, 0, 0, 0, 1, 1, 1] > etc. > > I was looking at Array#pack and Array#unpack to do this, but > haven't figured > it out yet. It just seems like there ought to be a simple way to do > this. > > Anybody got a clever idea? Last idea: >> bits = Array.new(8) { |i| 7[i] }.reverse! => [0, 0, 0, 0, 0, 1, 1, 1] I promise to stop spamming this thread now. ;) James Edward Gray II