--- /dev/null
+# Copyright (C) 2007-2011 Martin A. Hansen.
+
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# http://www.gnu.org/copyleft/gpl.html
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# This software is part of the Biopieces framework (www.biopieces.org).
+
+# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+# Monkey patching Class String to add bitwise operators.
+# Behaviour matching Perl's:
+# http://perldoc.perl.org/perlop.html#Bitwise-String-Operators
+class String
+ # Method that performs bitwise AND operation where bits
+ # are copied if they exists in BOTH operands. If the operand
+ # sizes are different, the & operator methods acts as though
+ # the longer operand were truncated to the length of the shorter.
+ def &(str)
+ new = ""
+
+ (0 ... [self.length, str.length].min).each do |i|
+ new << (self[i].ord & str[i].ord)
+ end
+
+ new
+ end
+
+ # Method that performs bitwise OR operation where bits
+ # are copied if they exists in EITHER operands. If the operand
+ # sizes differ, the shorter operand is extended with the terminal
+ # part of the longer operand.
+ def |(str)
+ new = ""
+
+ min = [self.length, str.length].min
+
+ (0 ... min).each do |i|
+ new << (self[i].ord | str[i].ord)
+ end
+
+ if self.length > str.length
+ new << self[min ... self.length]
+ elsif self.length < str.length
+ new << str[min ... str.length]
+ end
+
+ new
+ end
+
+ # Method that performs bitwise XOR operation where bits
+ # are copied if they exists in ONE BUT NOT BOTH operands.
+ def ^(str)
+ new = ""
+
+ (0 ... [self.length, str.length].min).each do |i|
+ new << (self[i].ord ^ str[i].ord)
+ end
+
+ new
+ end
+end
--- /dev/null
+#!/usr/bin/env ruby
+
+require 'bits'
+require 'test/unit'
+require 'pp'
+
+class TestBits < Test::Unit::TestCase
+ def test_Bits_AND_with_equal_length_returns_correctly
+ assert_equal("ABCD", "abcd" & "____")
+ end
+
+ def test_Bits_AND_with_unequal_length_returns_correctly
+ assert_equal("JAPH\n", "japh\nJunk" & '_____')
+ assert_equal("JAPH\n", '_____' & "japh\nJunk")
+ end
+
+ def test_Bits_OR_with_equal_length_returns_correctly
+ assert_equal("abcd", "ab " | " cd")
+ end
+
+ def test_Bits_OR_with_unequal_length_returns_correctly
+ assert_equal("japh\n", "JA" | " ph\n")
+ assert_equal("japh\n", " ph\n" | "JA")
+ end
+
+ def test_Bits_XOR_with_equal_length_returns_correctly
+ assert_equal("ABCD", "ab " ^ " cd")
+ end
+
+ def test_Bits_XOR_with_unequal_length_returns_correctly
+ assert_equal("JAPH", "j p \n" ^ " a h")
+ assert_equal("JAPH", " a h" ^ "j p \n")
+ end
+end