[](p1)
public
Bit
Reference—Returns the nth bit in the (assumed) binary
representation of big, where big[0] is the least
significant bit.
a = 9**15
50.downto(0) do |n|
print a[n]
end
produces:
000101110110100000111000011110010100111100010111001
Show source
static VALUE
rb_big_aref(VALUE x, VALUE y)
{
BDIGIT *xds;
unsigned long shift;
long i, s1, s2;
BDIGIT bit;
if (RB_BIGNUM_TYPE_P(y)) {
if (!RBIGNUM_SIGN(y))
return INT2FIX(0);
bigtrunc(y);
if (BIGSIZE(y) > sizeof(long)) {
out_of_range:
return RBIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1);
}
shift = big2ulong(y, "long");
}
else {
i = NUM2LONG(y);
if (i < 0) return INT2FIX(0);
shift = i;
}
s1 = shift/BITSPERDIG;
s2 = shift%BITSPERDIG;
bit = (BDIGIT)1 << s2;
if (s1 >= RBIGNUM_LEN(x)) goto out_of_range;
xds = BDIGITS(x);
if (RBIGNUM_POSITIVE_P(x))
return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
if (xds[s1] & (bit-1))
return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
for (i = 0; i < s1; i++)
if (xds[i])
return (xds[s1] & bit) ? INT2FIX(0) : INT2FIX(1);
return (xds[s1] & bit) ? INT2FIX(1) : INT2FIX(0);
}