Sha256: 8c05e0ebfe573fa9b3a8cf598463bd8b735e27454f2ae28663845757be8b01ed

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

class Bitsy
  class Mask

    attr_reader :flag, :value

    def self.with_index(flag, idx)
      self.new(flag, (1 << idx))
    end

    def initialize(flag, value)
      @flag = flag
      @value = value
    end

    def to_i
      value
    end

    def &(other)
      self.class.new(combine_flag(other, 'AND'), value & other.to_i)
    end

    def |(other)
      self.class.new(combine_flag(other, 'OR'), value | other.to_i)
    end

    def ^(other)
      self.class.new(combine_flag(other, 'XOR'), value ^ other.to_i)
    end

    def ~
      self.class.new("NOT_#{flag}".to_sym, ~value)
    end

    private

    def combine_flag(other, operation)
      if other.respond_to?(:flag)
        "#{flag}_#{operation}_#{other.flag}".to_sym
      else
        "#{flag}_#{operation}_#{other}".to_sym
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bitsy-0.0.1 lib/bitsy/mask.rb