[](p1)
public
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
Show source
/*
* 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 (!FIXNUM_P(idx = fix_coerce(idx))) {
idx = rb_big_norm(idx);
if (!FIXNUM_P(idx)) {
if (!RBIGNUM(idx)->sign || val >= 0)
return INT2FIX(0);
return INT2FIX(1);
}
}
i = FIX2LONG(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);
}