A Fixnum holds Integer values that can be represented in a native machine word (minus 1 bit). If any operation on a Fixnum exceeds this range, the value is automatically converted to a Bignum.

Fixnum objects have immediate value. This means that when they are assigned or passed as parameters, the actual object is passed, rather than a reference to that object. Assignment does not alias Fixnum objects. There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum.

Methods
#
A
D
E
F
I
M
O
P
Q
R
S
T
Z
#
Included Modules
Class Public methods
Fixnum.induced_from(obj) => fixnum

Convert obj to a Fixnum. Works with numeric parameters. Also works with Symbols, but this is deprecated.

static VALUE
rb_fix_induced_from(klass, x)
    VALUE klass, x;
{
    return rb_num2fix(x);
}
Instance Public methods
fix % other => Numeric fix.modulo(other) => Numeric

Returns fix modulo other. See Numeric.divmod for more information.

static VALUE
fix_mod(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long mod;

        fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
        return LONG2NUM(mod);
    }
    return rb_num_coerce_bin(x, y);
}
fix & other => integer

Bitwise AND.

static VALUE
fix_and(x, y)
    VALUE x, y;
{
    long val;

    if (!FIXNUM_P(y = fix_coerce(y))) {
        return rb_big_and(y, x);
    }
    val = FIX2LONG(x) & FIX2LONG(y);
    return LONG2NUM(val);
}
fix * numeric => numeric_result

Performs multiplication: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

static VALUE
fix_mul(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
#ifdef __HP_cc
        /* avoids an optimization bug of HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005] */
        volatile
#endif
        long a, b, c;
        VALUE r;

        a = FIX2LONG(x);
        if (a == 0) return x;

        b = FIX2LONG(y);
        c = a * b;
        r = LONG2FIX(c);

        if (FIX2LONG(r) != c || c/a != b) {
            r = rb_big_mul(rb_int2big(a), rb_int2big(b));
        }
        return r;
    }
    if (TYPE(y) == T_FLOAT) {
        return rb_float_new((double)FIX2LONG(x) * RFLOAT(y)->value);
    }
    return rb_num_coerce_bin(x, y);
}
**(other)

Alias for rpower

fix ** other => Numeric

Raises fix to the other power, which may be negative or fractional.

  2 ** 3      #=> 8
  2 ** -1     #=> 0.5
  2 ** 0.5    #=> 1.4142135623731
This method is also aliased as power!
static VALUE
fix_pow(x, y)
    VALUE x, y;
{
    static const double zero = 0.0;
    long a = FIX2LONG(x);

    if (FIXNUM_P(y)) {
        long b = FIX2LONG(y);

        if (b == 0) return INT2FIX(1);
        if (b == 1) return x;
        if (a == 0) {
            if (b > 0) return INT2FIX(0);
            return rb_float_new(1.0 / zero);
        }
        if (a == 1) return INT2FIX(1);
        if (a == -1) {
            if (b % 2 == 0)
                return INT2FIX(1);
            else 
                return INT2FIX(-1);
        }
        if (b > 0) {
            return int_pow(a, b);
        }
        return rb_float_new(pow((double)a, (double)b));
    }
    switch (TYPE(y)) {
      case T_BIGNUM:
        if (a == 0) return INT2FIX(0);
        if (a == 1) return INT2FIX(1);
        if (a == -1) {
            if (int_even_p(y)) return INT2FIX(1);
            else return INT2FIX(-1);
        }
        x = rb_int2big(FIX2LONG(x));
        return rb_big_pow(x, y);
      case T_FLOAT:
        if (RFLOAT(y)->value == 0.0) return rb_float_new(1.0);
        if (a == 0) {
            return rb_float_new(RFLOAT(y)->value < 0 ? (1.0 / zero) : 0.0);
        }
        if (a == 1) return rb_float_new(1.0);
        return rb_float_new(pow((double)a, RFLOAT(y)->value));
      default:
        return rb_num_coerce_bin(x, y);
    }
}
fix + numeric => numeric_result

Performs addition: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

static VALUE
fix_plus(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a, b, c;
        VALUE r;

        a = FIX2LONG(x);
        b = FIX2LONG(y);
        c = a + b;
        r = LONG2NUM(c);

        return r;
    }
    if (TYPE(y) == T_FLOAT) {
        return rb_float_new((double)FIX2LONG(x) + RFLOAT(y)->value);
    }
    return rb_num_coerce_bin(x, y);
}
fix - numeric => numeric_result

Performs subtraction: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

static VALUE
fix_minus(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a, b, c;
        VALUE r;

        a = FIX2LONG(x);
        b = FIX2LONG(y);
        c = a - b;
        r = LONG2NUM(c);

        return r;
    }
    if (TYPE(y) == T_FLOAT) {
        return rb_float_new((double)FIX2LONG(x) - RFLOAT(y)->value);
    }
    return rb_num_coerce_bin(x, y);
}
-fix => integer

Negates fix (which might return a Bignum).

static VALUE
fix_uminus(num)
    VALUE num;
{
    return LONG2NUM(-FIX2LONG(num));
}
fix / numeric => numeric_result fix.div(numeric) => numeric_result

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

static VALUE
fix_div(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long div;

        fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
        return LONG2NUM(div);
    }
    return rb_num_coerce_bin(x, y);
}
/(p1)

Alias for quo

fix < other => true or false

Returns true if the value of fix is less than that of other.

static VALUE
fix_lt(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a = FIX2LONG(x), b = FIX2LONG(y);

        if (a < b) return Qtrue;
        return Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y);
    }
}
fix << count => integer

Shifts fix left count positions (right if count is negative).

static VALUE
rb_fix_lshift(x, y)
    VALUE x, y;
{
    long val, width;

    val = NUM2LONG(x);
    if (!FIXNUM_P(y))
        return rb_big_lshift(rb_int2big(val), y);
    width = FIX2LONG(y);
    if (width < 0)
        return fix_rshift(val, (unsigned long)-width);
    return fix_lshift(val, width);
}
fix <= other => true or false

Returns true if the value of fix is less thanor equal to that of other.

static VALUE
fix_le(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a = FIX2LONG(x), b = FIX2LONG(y);

        if (a <= b) return Qtrue;
        return Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y);
    }
}
fix <=> numeric => -1, 0, +1

Comparison—Returns -1, 0, or +1 depending on whether fix is less than, equal to, or greater than numeric. This is the basis for the tests in Comparable.

static VALUE
fix_cmp(x, y)
    VALUE x, y;
{
    if (x == y) return INT2FIX(0);
    if (FIXNUM_P(y)) {
        long a = FIX2LONG(x), b = FIX2LONG(y);

        if (a > b) return INT2FIX(1);
        return INT2FIX(-1);
    }
    else {
        return rb_num_coerce_cmp(x, y);
    }
}
fix == other

Return true if fix equals other numerically.

  1 == 2      #=> false
  1 == 1.0    #=> true
static VALUE
fix_equal(x, y)
    VALUE x, y;
{
    if (x == y) return Qtrue;
    if (FIXNUM_P(y)) return Qfalse;
    return num_equal(x, y);
}
fix > other => true or false

Returns true if the value of fix is greater than that of other.

static VALUE
fix_gt(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a = FIX2LONG(x), b = FIX2LONG(y);

        if (a > b) return Qtrue;
        return Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y);
    }
}
fix >= other => true or false

Returns true if the value of fix is greater than or equal to that of other.

static VALUE
fix_ge(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long a = FIX2LONG(x), b = FIX2LONG(y);

        if (a >= b) return Qtrue;
        return Qfalse;
    }
    else {
        return rb_num_coerce_relop(x, y);
    }
}
fix >> count => integer

Shifts fix right count positions (left if count is negative).

static VALUE
rb_fix_rshift(x, y)
    VALUE x, y;
{
    long i, val;

    val = FIX2LONG(x);
    if (!FIXNUM_P(y))
        return rb_big_rshift(rb_int2big(val), y);
    i = FIX2LONG(y);
    if (i == 0) return x;
    if (i < 0)
        return fix_lshift(val, (unsigned long)-i);
    return fix_rshift(val, i);
}
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
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);
}
fix ^ other => integer

Bitwise EXCLUSIVE OR.

static VALUE
fix_xor(x, y)
    VALUE x, y;
{
    long val;

    if (!FIXNUM_P(y = fix_coerce(y))) {
        return rb_big_xor(y, x);
    }
    val = FIX2LONG(x) ^ FIX2LONG(y);
    return LONG2NUM(val);
}
fix.abs → aFixnum

Returns the absolute value of fix.

   -12345.abs   #=> 12345
   12345.abs    #=> 12345
static VALUE
fix_abs(fix)
    VALUE fix;
{
    long i = FIX2LONG(fix);

    if (i < 0) i = -i;

    return LONG2NUM(i);
}
dclone()
    # File lib/rexml/xpath_parser.rb, line 16
16:   def dclone ; self ; end
fix / numeric => numeric_result fix.div(numeric) => numeric_result

Performs division: the class of the resulting object depends on the class of numeric and on the magnitude of the result.

static VALUE
fix_div(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long div;

        fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, 0);
        return LONG2NUM(div);
    }
    return rb_num_coerce_bin(x, y);
}
fix.divmod(numeric) => array
static VALUE
fix_divmod(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long div, mod;

        fixdivmod(FIX2LONG(x), FIX2LONG(y), &div, &mod);

        return rb_assoc_new(LONG2NUM(div), LONG2NUM(mod));
    }
    return rb_num_coerce_bin(x, y);
}
fix.even? → true or false

Returns true if fix is an even number.

static VALUE
fix_even_p(VALUE num)
{
    if (num & 2) {
        return Qfalse;
    }
    return Qtrue;
}
fix.quo(numeric) => float fix.fdiv(numeric) => float

Returns the floating point result of dividing fix by numeric.

   654321.quo(13731)      #=> 47.6528293642124
   654321.quo(13731.24)   #=> 47.6519964693647
static VALUE
fix_quo(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y));
    }
    return rb_num_coerce_bin(x, y);
}
fix.id2name → string or nil

Returns the name of the object whose symbol id is fix. If there is no symbol in the symbol table with this value, returns nil. id2name has nothing to do with the Object.id method. See also Fixnum#to_sym, String#intern, and class Symbol.

   symbol = :@inst_var    #=> :@inst_var
   id     = symbol.to_i   #=> 9818
   id.id2name             #=> "@inst_var"
static VALUE
fix_id2name(fix)
    VALUE fix;
{
    const char *name = rb_id2name(FIX2UINT(fix));
    if (name) return rb_str_new2(name);
    return Qnil;
}
fix % other => Numeric fix.modulo(other) => Numeric

Returns fix modulo other. See Numeric.divmod for more information.

static VALUE
fix_mod(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        long mod;

        fixdivmod(FIX2LONG(x), FIX2LONG(y), 0, &mod);
        return LONG2NUM(mod);
    }
    return rb_num_coerce_bin(x, y);
}
fix.odd? → true or false

Returns true if fix is an odd number.

static VALUE
fix_odd_p(VALUE num)
{
    if (num & 2) {
        return Qtrue;
    }
    return Qfalse;
}
power!(p1)

Alias for #**

quo(other)

If Rational is defined, returns a Rational number instead of a Float.

     # File lib/rational.rb, line 519
519:   def quo(other)
520:     Rational.new!(self, 1) / other
521:   end
fix.quo(numeric) => float fix.fdiv(numeric) => float

Returns the floating point result of dividing fix by numeric.

   654321.quo(13731)      #=> 47.6528293642124
   654321.quo(13731.24)   #=> 47.6519964693647
This method is also aliased as rdiv /
static VALUE
fix_quo(x, y)
    VALUE x, y;
{
    if (FIXNUM_P(y)) {
        return rb_float_new((double)FIX2LONG(x) / (double)FIX2LONG(y));
    }
    return rb_num_coerce_bin(x, y);
}
rdiv(p1)

Alias for quo

rpower(other)

Returns a Rational number if the result is in fact rational (i.e. other < 0).

This method is also aliased as **
     # File lib/rational.rb, line 525
525:   def rpower (other)
526:     if other >= 0
527:       self.power!(other)
528:     else
529:       Rational.new!(self, 1)**other
530:     end
531:   end
fix.size → fixnum

Returns the number of bytes in the machine representation of a Fixnum.

   1.size            #=> 4
   -1.size           #=> 4
   2147483647.size   #=> 4
static VALUE
fix_size(fix)
    VALUE fix;
{
    return INT2FIX(sizeof(long));
}
fix.to_f → float

Converts fix to a Float.

static VALUE
fix_to_f(num)
    VALUE num;
{
    double val;

    val = (double)FIX2LONG(num);

    return rb_float_new(val);
}
fix.to_s( base=10 ) → aString

Returns a string containing the representation of fix radix base (between 2 and 36).

   12345.to_s       #=> "12345"
   12345.to_s(2)    #=> "11000000111001"
   12345.to_s(8)    #=> "30071"
   12345.to_s(10)   #=> "12345"
   12345.to_s(16)   #=> "3039"
   12345.to_s(36)   #=> "9ix"
static VALUE
fix_to_s(argc, argv, x)
    int argc;
    VALUE *argv;
    VALUE x;
{
    VALUE b;
    int base;

    rb_scan_args(argc, argv, "01", &b);
    if (argc == 0) base = 10;
    else base = NUM2INT(b);

    return rb_fix2str(x, base);
}
fix.to_sym → aSymbol

Returns the symbol whose integer value is fix. See also Fixnum#id2name.

   fred = :fred.to_i
   fred.id2name   #=> "fred"
   fred.to_sym    #=> :fred
static VALUE
fix_to_sym(fix)
    VALUE fix;
{
    ID id = FIX2UINT(fix);

    if (rb_id2name(id)) {
        return ID2SYM(id);
    }
    return Qnil;
}
fix.zero? => true or false

Returns true if fix is zero.

static VALUE
fix_zero_p(num)
    VALUE num;
{
    if (FIX2LONG(num) == 0) {
        return Qtrue;
    }
    return Qfalse;
}
fix | other => integer

Bitwise OR.

static VALUE
fix_or(x, y)
    VALUE x, y;
{
    long val;

    if (!FIXNUM_P(y = fix_coerce(y))) {
        return rb_big_or(y, x);
    }
    val = FIX2LONG(x) | FIX2LONG(y);
    return LONG2NUM(val);
}
~fix => integer

One’s complement: returns a number where each bit is flipped.

static VALUE
fix_rev(num)
    VALUE num;
{
    long val = FIX2LONG(num);

    val = ~val;
    return LONG2NUM(val);
}