1
votes

I want use bitwise operator "&" with string like that :

 raw_counter_int = raw_counter.to_i
 raw_counter_bin = raw_counter_int.to_s(2)
 u = (2**62 + 2**63)
 k = u.to_s(2)
 r =  raw_counter_bin & k
 @counter_msg = r

but when I run my application I've this error message :

undefined method `&' for "10000000000000000000000000000000000000000000000000000000":String

How I can use this operator "&" with raw_counter_int and u which are converted in binary ?

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

1
Don't need string conversion here raw_counter_int.to_s(2) I think. - Arup Rakshit
Well, you convert it into a string, what do you expect? & works on numbers, not strings. It's already an integer, just use &. - Dave Newton
Why ? When I delete .to_s(2) on raw_counter_bin and k, I've that 0, so I do 652144440 & (262 + 263) and I want do 10000000000000000000 000000000000000000000000000000000000 & 1100000000000000000000000000000000000000000000000000000000000000 - Matarela
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 AND with 0000 0000 1000 0000 0000 0000 0000 0000 & 0011 1111 1100 0000 0000 0000 0000 0000 to take just this : 00 0000 10 - Matarela
One bug I found, not related to this. But related to bit-wise operation ruby-forum.com/topic/85756 - Arup Rakshit

1 Answers

1
votes

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...