From: Caleb Clausen Date: 2010-01-04T16:38:17+09:00 Subject: Re: bitwise shift help On 1/3/10, Kvetch Kvetch wrote: > Kvetch Kvetch wrote: >> Hello, I am new to Ruby and was wondering if there is an easier/faster >> way to determine if a bit is set in a number. For example, [snip] >> y = 1 << 3 >> x = 0b1011000010 >> >> if ( x & y ) == 1 >> puts "The 3rd bit is set" >> else >> puts "The 3rd bit is not set" >> end >> >> Is there a better/faster method of achieving this bit check? > > Actually I suppose the bit check should read > if ( x & y ) >= 1 > Or I am still totally off. There's nonzero?, which ought to be a tad faster than >=1. So, write this instead: if ( x & y ).nonzero? Or, if you incorporate Axel's suggestion to use Fixnum#[] as well, then: if x[3].nonzero?