public Method

Fixnum.[](p1)

fix[n]      0, 1

Bit Reference—Returns the nth bit in the binary representation of fix, where fix[0] is the least significant bit.

a = 0b11001100101010
30.downto(0) do |n| print a[n] end

produces:

0000000000000000011001100101010

Source Code

/*
*  call-seq:
*     fix[n]     => 0, 1
*  
*  Bit Reference---Returns the <em>n</em>th bit in the binary
*  representation of <i>fix</i>, where <i>fix</i>[0] is the least
*  significant bit.
*     
*     a = 0b11001100101010
*     30.downto(0) do |n| print a[n] end
*     
*  <em>produces:</em>
*     
*     0000000000000000011001100101010
*/

static VALUE
fix_aref(fix, idx)
   VALUE fix, idx;
{
   long val = FIX2LONG(fix);
   long i;

   if (TYPE(idx) == T_BIGNUM) {
       idx = rb_big_norm(idx);
       if (!FIXNUM_P(idx)) {
           if (!RBIGNUM(idx)->sign || val >= 0)
               return INT2FIX(0);
           return INT2FIX(1);
       }
   }
   i = NUM2LONG(idx);

   if (i < 0) return INT2FIX(0);
   if (sizeof(VALUE)*CHAR_BIT-1 < i) {
       if (val < 0) return INT2FIX(1);
       return INT2FIX(0);
   }
   if (val & (1L<<i))
       return INT2FIX(1);
   return INT2FIX(0);
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.