Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top