1

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

6
  • Don't need string conversion here raw_counter_int.to_s(2) I think. Commented Mar 25, 2014 at 14:58
  • Well, you convert it into a string, what do you expect? & works on numbers, not strings. It's already an integer, just use &. Commented Mar 25, 2014 at 15:01
  • 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 Commented Mar 25, 2014 at 15:05
  • 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 Commented Mar 25, 2014 at 15:11
  • One bug I found, not related to this. But related to bit-wise operation ruby-forum.com/topic/85756 Commented Mar 25, 2014 at 15:21

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.