Hi,

Am Montag, 04. Jan 2010, 13:38:08 +0900 schrieb Kvetch Kvetch:
> 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,
> If my number is 1011000010 and I want to check to see if 3rd bit is set
> so
> 1011000010
> 1000010000
> ----------
>       0000 - So 3rd bit is not set
> My code is
> 
> 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?
> Thank you

As in SQL, 0 does not yield false. SQL's NULL is different from
0 and from ''. So is in Ruby `nil' different from 0, from "", and
from [], and further from {}. In C you're free to write

  if (num % 7) { ... }

That won't work in Ruby.

`Numeric#nonzero?' has been mentioned. Of course, I cannot resist
and just mention my beloved proposal of `String#notempty?',
`Array#notempty?', and `Hash#notempty?'.

`Bertram::NotEmpty#insists?' still returns `true'.

Bertram


-- 
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Support `String#notempty?': <http://raa.ruby-lang.org/project/step>.