[](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;
size_t shift;
size_t i, s1, s2;
long l;
BDIGIT bit;
if (RB_BIGNUM_TYPE_P(y)) {
if (!BIGNUM_SIGN(y))
return INT2FIX(0);
bigtrunc(y);
if (BIGSIZE(y) > sizeof(size_t)) {
out_of_range:
return BIGNUM_SIGN(x) ? INT2FIX(0) : INT2FIX(1);
}
shift = big2ulong(y, "long");
shift = big2ull(y, "long long");
}
else {
l = NUM2LONG(y);
if (l < 0) return INT2FIX(0);
shift = (size_t)l;
}
s1 = shift/BITSPERDIG;
s2 = shift%BITSPERDIG;
bit = (BDIGIT)1 << s2;
if (s1 >= BIGNUM_LEN(x)) goto out_of_range;
xds = BDIGITS(x);
if (BIGNUM_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);
}