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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2009-Dec-04 20:31 UTC
Re: Trying to get the value of a single bit in a variable
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 <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>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 Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.