Bitwise Operations on Strings with Ruby

Use bitwise AND with string on ruby on rails

I try with this: 0000 0000 1000 0000 0000 0000 0000 0000 (64 bits) to
take bytes between the third bytes and the 10th bytes. So I want do a
bitwise "&" with 0000 0000 1000 0000 0000 0000 0000 0000 & 0011 1111
1100 0000 0000 0000 0000 0000 to take just this : 00 0000 10

Let's do it:

("00000000100000000000000000000000".to_i(2) & "00111111110000000000000000000000".to_i(2)).to_s(2)
=> "100000000000000000000000"

Which is exactly what is expected! The number shown in the error ("10000000000000000000000000000000000000000000000000000000") is 2^56, which, when using bitwise AND with it and 2^62+2^63 is expected to give you a zero result...

I suggest you check your input again, and trust ruby's & to do the job...

bitwise operations on strings with ruby

If you are always going to operate on 4-byte strings, String#unpack with an argument of 'V' will treat four bytes as an unsigned long in little-endian byte order. Using 'N' will force network byte order, and using 'L' will use native order. Note that unpack always returns an array, thus the need to take index 0.

>> '0x%X' % ('abcd'.unpack('V')[0] & 0xDA2DFFD3)
=> "0x40216241"

If it's not always four bytes, you can call String#bytes to get the bytes of the string, and then you can use Enumerable#inject to accumulate the bytes into a number.

>> "abcd".bytes.inject {|x, y| (x << 8) | y}
=> 1633837924
>> "abcd".bytes.inject {|x, y| (x << 8) | y}.to_s(16)
=> "61626364"
>> "abcd".bytes.inject {|x, y| (x << 8) | y} & 0xDA2DFFD3
=> 1075864384
>> "0x%X" % ("abcd".bytes.inject {|x, y| (x << 8) | y} & 0xDA2DFFD3)
=> "0x40206340"

This is "safe" as long as you're using ASCII strings. If you start using multi-byte strings, you're going to get "odd" results.

How to bitwise AND two bit strings in Ruby?

The following code should do what you are looking for:

str  = "11111111010011111111111010000001"
str2 = "11000000000000000000000000000011"

result = str.to_i(2) & str2.to_i(2)

result.to_s(2)
=> "11000000000000000000000000000001"

Ruby: Doing binary operations on bitstrings

You have Integer#to_s(base) and String.to_i(base) available to you.

a_and_b = (a.to_i(2) & b.to_i(2)).to_s(2)

a_or_b = (a.to_i(2) | b.to_i(2)).to_s(2)

Bitwise operation on Ruby string

1U in the C++ is not a string, it is the unsigned number 1. In fact, the code above in C++ could be substituted by:

a = 0;
b = 1U;
c = 2U;
d = 4U;

In ruby you can simply do

> 1 << 0
=> 1 #0001
> 1 << 1
=> 2 #0010
> 1 << 2
=> 4 #0100

But you are not using bytewise operations in ruby unless you have a very good reason for it, right? :-)

Use bitwise AND with string on ruby on rails

I try with this: 0000 0000 1000 0000 0000 0000 0000 0000 (64 bits) to
take bytes between the third bytes and the 10th bytes. So I want do a
bitwise "&" with 0000 0000 1000 0000 0000 0000 0000 0000 & 0011 1111
1100 0000 0000 0000 0000 0000 to take just this : 00 0000 10

Let's do it:

("00000000100000000000000000000000".to_i(2) & "00111111110000000000000000000000".to_i(2)).to_s(2)
=> "100000000000000000000000"

Which is exactly what is expected! The number shown in the error ("10000000000000000000000000000000000000000000000000000000") is 2^56, which, when using bitwise AND with it and 2^62+2^63 is expected to give you a zero result...

I suggest you check your input again, and trust ruby's & to do the job...

Binary or | in ruby

Yes, the bitwise operator | is not defined in the String class: http://ruby-doc.org/core/classes/String.html

Consider this for expressiveness:

["hi", "ho"].include? myStr

irb(main):001:0> s = "hi"
=> "hi"
irb(main):002:0> ["hi", "ho"]
=> ["hi", "ho"]
irb(main):003:0> ["hi", "ho"].include? s
=> true
irb(main):004:0> s = "foo"
=> "foo"
irb(main):005:0> ["hi", "ho"].include? s
=> false

Convert int to binary, the perform bitwise operations on it

the_number.to_s(2).split('').map { |x| x.to_i } # convert the number to binary array

Ensuring variable size for bitwise operations in Ruby

Ruby has unlimited integers, so don't worry about that. You won't lose a single bit.

a = 0

a |= (1 << 200)

a # => 1606938044258990275541962092341162602522202993782792835301376


Related Topics



Leave a reply



Submit