Trying to get the value of a single bit in a variable

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 bsm2th@gmail.com

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 bsm2th@gmail.com

Well, Integers support bit operations.

weekly = 0

=> 0

weekly[4]

=> 0

weekly |= 1 << 4

=> 16

weekly[4]

=> 1

weekly.class

=> Fixnum

weekly |= 1 << 52

=> 4503599627370512

weekly.class

=> Bignum

weekly.to_s(2)

=> “10000000000000000000000000000000000000000000000010000”

Note that bit0 is the least significant.

-Rob

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com