Rotation bit. Proper work in Ruby

Is there a bit to the right in Ruby?

Or how can I do this.

thank

+3
source share
3 answers

Some facts:

  • In Ruby there are operators <<and >>to switch, but there is no built-in rotation operator. You have to fake it.
  • The Ruby class Fixnumautomatically advances to Bignumwhen the value exceeds the size of a machine word. This includes numbers that match an unsigned word but not an unsigned word — for example, it 0xffffffffis positive Bignum, not negative Fixnum.

, , , b) 32 64 , Fixnum , c) , .

:

class Integer
  def ror count
    (self >> count) | (self << (32 - count)) & 0xFFFFFFFF
  end
end
>> printf "0x%x\n", (0x01234567.ror 4)
0x70123456
+14

, bit-twiddle gem, , :

require 'bit-twiddle/core_ext'
# rotate by 8 bits
0x08048586.rrot32(8).to_s(16) # => "86080485"

: .

+1

ROR .

C-, Ruby.

Ruby

  • < <

0

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


All Articles