public
Created

rubyでビット文字列から'1'の位置を求める ref: http://qiita.com/items/0726851e110e2e356791

  • Download Gist
scan_bits_index.rb
Ruby
1 2
"10100100110".reverse.split('1',-1).inject([]){|ret,i| ret << i.size + (ret.last.nil? ? 0 : ret.last + 1); ret }[0..-2]
# => [1, 2, 5, 8, 10]
scan_bits_index_from_left.rb
Ruby
1 2
"10100100110".split('1',-1).inject([]){|ret,i| ret << i.size + (ret.last.nil? ? 0 : ret.last + 1); ret }[0..-2]
# => [0, 2, 5, 8, 9]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.