class String
  def ^(other)
    raise ArgumentError, "Can't bitwise-XOR a String with a non-String" \
      unless other.kind_of? String
    raise ArgumentError, "Can't bitwise-XOR strings of different length" \
      unless self.length == other.length
    result = (0..self.length-1).collect { |i| self[i] ^ other[i] }
    result.pack("C*")
  end
  def &(other)
    raise ArgumentError, "Can't bitwise-AND a String with a non-String" \
      unless other.kind_of? String
    raise ArgumentError, "Can't bitwise-AND strings of different length" \
      unless self.length == other.length
    result = (0..self.length-1).collect { |i| self[i] & other[i] }
    result.pack("C*")
  end
  def |(other)
    raise ArgumentError, "Can't bitwise-OR a String with a non-String" \
      unless other.kind_of? String
    raise ArgumentError, "Can't bitwise-OR strings of different length" \
      unless self.length == other.length
    result = (0..self.length-1).collect { |i| self[i] | other[i] }
    result.pack("C*")
  end
  def ~
    result = (0..self.length-1).collect { |i| ~ self[i] }
    result.pack("C*")
  end
end
  Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog