Monkey patching class String to add bitwise operators

M

Martin Hansen

Hello all,


Unfortunately Ruby don't have bitwise operators for Class String like
Perl do. I am hoping very much that such operators will be implemented
in Ruby one day at a low level so they will fast! But until such a day,
I will roll my own crude operators by monkey patching Class String.
However, I am experiencing a peculiar error from my Unit Tests:



Loaded suite ./test_bits
Started
....F.
Finished in 0.000661 seconds.

1) Failure:
test_Bits_XOR_with_equal_length_returns_correctly(TestBits)
[./test_bits.rb:27]:
<"00110001"> expected but was
<"\x00\x00\x01\x01\x00\x00\x00\x01">.

6 tests, 9 assertions, 1 failures, 0 errors, 0 skips

Test run options: --seed 36958




Code:



# 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.ord & str.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.ord | str.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.ord ^ str.ord)
end

new
end
end


Tests:


#!/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("00001100", "00111100" & "00001101")
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("00111101", "00111100" | "00001101")
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("00110001", "00111100" ^ "00001101")
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


So, what is this error I see?



Cheers,


Martin
 
M

Martin Hansen

Sorry for the delayed answer, I have been away.

I fail to see how I can fix my code. Adding chr doesnt do the trick
(same error):

new << (self.ord ^ str.ord).chr



Cheers,


Martin
 
J

Jeremy Bopp

Sorry for the delayed answer, I have been away.

I fail to see how I can fix my code. Adding chr doesnt do the trick
(same error):

new << (self.ord ^ str.ord).chr


You can't just call chr on the result if you want the character to be
'0' or '1'.

irb(main):001:0> '0'[0].ord ^ '1'[0].ord
=> 1

Like Ryan said, the ascii value for the character '1' is actually 49;
however, you have the value of 1, the number rather than the string.
You need to add 48 to the result of your operation if you want to
actually get '1'.

irb(main):002:0> ('0'[0].ord ^ '1'[0].ord) + 48
=> 49

You can then call chr on that.

irb(main):003:0> (('0'[0].ord ^ '1'[0].ord) + 48).chr
=> "1"

Doing this is going to break your other tests though since most of them
expect to be working with letters instead of numbers.

Your problem appears to be with how you expect to represent "binary"
strings vs. regular strings. You seem to want strings that look like
'1011011' to actually be converted into bit strings before processing
and then converted back to "binary" strings after. If you know
beforehand that you're going to work with a "binary" string though, you
can easily avoid all your extra work by using String#to_i and Fixnum#to_s.

irb(main):004:0> '1011011'.to_i(2)
=> 91
irb(main):005:0> '1011011'.to_i(2).to_s(2)
=> "1011011"
irb(main):006:0> '1011011'.to_i(2) ^ '0000100'.to_i(2)
=> 95
irb(main):007:0> ('1011011'.to_i(2) ^ '0000100'.to_i(2)).to_s(2)
=> "1011111"


-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top