On Dec 4, 2009, at 3:02 PM, Bob Smith wrote:
> I'm trying to use @weekly to represent attendance for each week of  
> the month. I wanted to use a bit for each week, but am having no  
> luck finding a way to get a single bit out of the variable after it  
> is set. I already have this code to set the values:
>
>     @mask = (2 ** @week_number)
>     params['household']['visit']['weekly'] = (@mask && @thisweek)
>
> Please help..
>
> Bob <[email protected]>

Well, Integers support bit operations.

irb> weekly = 0
=> 0
irb> weekly[4]
=> 0
irb> weekly |= 1 << 4
=> 16
irb> weekly[4]
=> 1
irb> weekly.class
=> Fixnum
irb> weekly |= 1 << 52
=> 4503599627370512
irb> weekly.class
=> Bignum
irb> weekly.to_s(2)
=> "10000000000000000000000000000000000000000000000010000"

Note that bit0 is the least significant.

-Rob

Rob Biedenharn          http://agileconsultingllc.com
[email protected]



--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to