mruby-numeric-ext/numeric.rb: bit predicates are implemented in Ruby now.
MODIFY
mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb
mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb
@@ -18,4 +18,34 @@ class Numeric
def negative?
self < 0
end
+
+ ##
+ # call-seq:
+ # int.allbits?(mask) -> true or false
+ #
+ # Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
+ #
+ def allbits?(mask)
+ (self & mask) == mask
+ end
+
+ ##
+ # call-seq:
+ # int.anybits?(mask) -> true or false
+ #
+ # Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
+ #
+ def anybits?(mask)
+ (self & mask) != 0
+ end
+
+ ##
+ # call-seq:
+ # int.nobits?(mask) -> true or false
+ #
+ # Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
+ #
+ def nobits?(mask)
+ (self & mask) == 0
+ end
end

MODIFY
mrbgems/mruby-numeric-ext/src/numeric_ext.c
mrbgems/mruby-numeric-ext/src/numeric_ext.c
#include <mruby/internal.h>
#include <mruby/presym.h>
-#ifdef MRB_USE_BIGINT
-static mrb_value
-bint_allbits(mrb_state *mrb, mrb_value x, mrb_value y)
-{
- y = mrb_as_bint(mrb, y);
- x = mrb_bint_and(mrb, x, y);
- if (mrb_bint_cmp(mrb, x, y) == 0) return mrb_true_value();
- return mrb_false_value();
-}
-
-static mrb_value
-bint_anybits(mrb_state *mrb, mrb_value x, mrb_value y)
-{
- y = mrb_as_bint(mrb, y);
- x = mrb_bint_and(mrb, x, y);
- if (mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) != 0)
- return mrb_true_value();
- return mrb_false_value();
-}
-
-static mrb_value
-bint_nobits(mrb_state *mrb, mrb_value x, mrb_value y)
-{
- y = mrb_as_bint(mrb, y);
- x = mrb_bint_and(mrb, x, y);
- if (mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) == 0)
- return mrb_true_value();
- return mrb_false_value();
-}
-#endif
-
-/*
- * call-seq:
- * int.allbits?(mask) -> true or false
- *
- * Returns +true+ if all bits of <code>+int+ & +mask+</code> are 1.
- */
-static mrb_value
-int_allbits(mrb_state *mrb, mrb_value self)
-{
-#ifdef MRB_USE_BIGINT
- return bint_allbits(mrb, self, mrb_get_arg1(mrb));
-#endif
- mrb_int n, m;
-
- mrb_get_args(mrb, "i", &m);
- n = mrb_integer(self);
- return mrb_bool_value((n & m) == m);
-}
-
-/*
- * call-seq:
- * int.anybits?(mask) -> true or false
- *
- * Returns +true+ if any bits of <code>+int+ & +mask+</code> are 1.
- */
-static mrb_value
-int_anybits(mrb_state *mrb, mrb_value self)
-{
-#ifdef MRB_USE_BIGINT
- return bint_anybits(mrb, self, mrb_get_arg1(mrb));
-#endif
- mrb_int n, m;
-
- mrb_get_args(mrb, "i", &m);
- n = mrb_integer(self);
- return mrb_bool_value((n & m) != 0);
-}
-
-/*
- * call-seq:
- * int.nobits?(mask) -> true or false
- *
- * Returns +true+ if no bits of <code>+int+ & +mask+</code> are 1.
- */
-static mrb_value
-int_nobits(mrb_state *mrb, mrb_value self)
-{
-#ifdef MRB_USE_BIGINT
- return bint_nobits(mrb, self, mrb_get_arg1(mrb));
-#endif
- mrb_int n, m;
-
- mrb_get_args(mrb, "i", &m);
- n = mrb_integer(self);
- return mrb_bool_value((n & m) == 0);
-}
-
#ifndef MRB_NO_FLOAT
static mrb_value flo_remainder(mrb_state *mrb, mrb_value self);
#endif
@@ -204,10 +116,6 @@ mrb_mruby_numeric_ext_gem_init(mrb_state* mrb)
{
struct RClass *i = mrb_class_get(mrb, "Integer");
- mrb_define_method(mrb, i, "allbits?", int_allbits, MRB_ARGS_REQ(1));
- mrb_define_method(mrb, i, "anybits?", int_anybits, MRB_ARGS_REQ(1));
- mrb_define_method(mrb, i, "nobits?", int_nobits, MRB_ARGS_REQ(1));
-
mrb_define_alias(mrb, i, "modulo", "%");
mrb_define_method(mrb, i, "remainder", int_remainder, MRB_ARGS_REQ(1));

mruby-numeric-ext/numeric.rb: bit predicates are implemented in Ruby now.