Little ultimate bitmask

I need to convert an array of integers to a small bot string using Ruby. Any links or tips would be appreciated.

example says [2,7,9,11] => "4205"

a = [2,7,9,11] # 4205
b = [1,2,3,4] # 0F00
def array_to_mask(arr)
  mask = 0
  arr.each do |i|
    mask = mask | (1 << i)
  end
  return mask.to_s(16)
end
p array_to_mask(a) # a84
p array_to_mask(b) # 1e

This does not work, but am I on the right track?

+3
source share
2 answers

Can't you use arr.pack () ? It has options for byte order.

Update: Good. I took a look at the documentation you mentioned and the only way to get this example to work:

  7          2              11   9      (decimal index count)
0 1 0 0  0 0 1 0  0 0 0 0  0 1 0 1      (bits)

   4        2        0        5         (nibbles, in hex)

, , 4205 - 4 , 2 ? ( ).

... " ", .

, - , , . () < or'ing | .

+2

Enfora , , . , , , .

values: [2,7,9,11]
Bits:   | 8 7 6 5 | 4 3 2 1 | 16 15 14 13 | 12 11 10 9 |
Binary: | 0 1 0 0 | 0 0 1 0 | 0  0  0  0  | 0  1  0  1 |
Hex:    |    4    |    2    |      0      |      5     |  
0

Source: https://habr.com/ru/post/1728222/


All Articles