% svn diff --diff-cmd diff -x '-u -p'
Index: bignum.c
===================================================================
--- bignum.c	(revision 42210)
+++ bignum.c	(working copy)
@@ -6318,6 +6318,88 @@ rb_big_size(VALUE big)
 
 /*
  *  call-seq:
+ *     big.bitsize -> integer
+ *
+ *  Returns the number of bits of the absolute value of <i>big</i>.
+ *
+ *     (-2**10000-1).bitsize    #=> 10001
+ *     (-2**10000).bitsize      #=> 10001
+ *     (-2**10000+1).bitsize    #=> 10000
+ *
+ *     (-2**1000-1).bitsize     #=> 1001
+ *     (-2**1000).bitsize       #=> 1001
+ *     (-2**1000+1).bitsize     #=> 1000
+ *
+ *     (2**1000-1).bitsize      #=> 1000
+ *     (2**1000).bitsize        #=> 1001
+ *     (2**1000+1).bitsize      #=> 1001
+ *
+ *     (2**10000-1).bitsize     #=> 10000
+ *     (2**10000).bitsize       #=> 10001
+ *     (2**10000+1).bitsize     #=> 10001
+ *
+ */
+
+static VALUE
+rb_big_bitsize(VALUE big)
+{
+    int nlz_bits;
+    size_t numbytes;
+
+    static const BDIGIT char_bit[1] = { CHAR_BIT };
+    BDIGIT numbytes_bary[bdigit_roomof(sizeof(size_t))];
+    BDIGIT nlz_bary[1];
+    BDIGIT result_bary[bdigit_roomof(sizeof(size_t)+1)];
+
+    numbytes = rb_absint_size(big, &nlz_bits);
+
+    if (numbytes <= SIZE_MAX / CHAR_BIT) {
+        return SIZET2NUM(numbytes * CHAR_BIT - nlz_bits);
+    }
+
+    nlz_bary[0] = nlz_bits;
+
+    bary_unpack(BARY_ARGS(numbytes_bary), &numbytes, 1, sizeof(numbytes), 0,
+            INTEGER_PACK_NATIVE_BYTE_ORDER);
+    BARY_MUL1(result_bary, numbytes_bary, char_bit);
+    BARY_SUB(result_bary, result_bary, nlz_bary);
+
+    return rb_integer_unpack(result_bary, numberof(result_bary), sizeof(BDIGIT), 0,
+            INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
+}
+
+/*
+ *  call-seq:
+ *     fix.bitsize -> integer
+ *
+ *  Returns the number of bits of the absolute value of <i>fix</i>.
+ *
+ *     (-2**12-1).bitsize)      #=> 13
+ *     (-2**12).bitsize)        #=> 13
+ *     (-2**12+1).bitsize)      #=> 12
+ *     -0x100.bitsize           #=> 9
+ *     -0xff.bitsize            #=> 8
+ *     -1.bitsize               #=> 1
+ *     0.bitsize                #=> 0
+ *     1.bitsize                #=> 1
+ *     0xff.bitsize             #=> 8
+ *     0x100.bitsize            #=> 9
+ *     (2**12-1).bitsize        #=> 12
+ *     (2**12).bitsize          #=> 13
+ *     (2**12+1).bitsize        #=> 13
+ */
+
+static VALUE
+rb_fix_bitsize(VALUE fix)
+{
+    long v = FIX2LONG(fix);
+    if (v < 0)
+        v = -v;
+    return LONG2FIX(bitsize(v));
+}
+
+/*
+ *  call-seq:
  *     big.odd? -> true or false
  *
  *  Returns <code>true</code> if <i>big</i> is an odd number.
@@ -6407,8 +6489,11 @@ Init_Bignum(void)
     rb_define_method(rb_cBignum, "abs", rb_big_abs, 0);
     rb_define_method(rb_cBignum, "magnitude", rb_big_abs, 0);
     rb_define_method(rb_cBignum, "size", rb_big_size, 0);
+    rb_define_method(rb_cBignum, "bitsize", rb_big_bitsize, 0);
     rb_define_method(rb_cBignum, "odd?", rb_big_odd_p, 0);
     rb_define_method(rb_cBignum, "even?", rb_big_even_p, 0);
 
+    rb_define_method(rb_cFixnum, "bitsize", rb_fix_bitsize, 0);
+
     power_cache_init();
 }
Index: test/ruby/test_fixnum.rb
===================================================================
--- test/ruby/test_fixnum.rb	(revision 42210)
+++ test/ruby/test_fixnum.rb	(working copy)
@@ -299,4 +299,20 @@ class TestFixnum < Test::Unit::TestCase
     assert_raise(ZeroDivisionError, bug5713) { 0 ** -big }
     assert_raise(ZeroDivisionError, bug5713) { 0 ** Rational(-2,3) }
   end
+
+  def test_bitsize
+    assert_equal(13, (-2**12-1).bitsize)
+    assert_equal(13, (-2**12).bitsize)
+    assert_equal(12, (-2**12+1).bitsize)
+    assert_equal(9, -0x100.bitsize)
+    assert_equal(8, -0xff.bitsize)
+    assert_equal(1, -1.bitsize)
+    assert_equal(0, 0.bitsize)
+    assert_equal(1, 1.bitsize)
+    assert_equal(8, 0xff.bitsize)
+    assert_equal(9, 0x100.bitsize)
+    assert_equal(12, (2**12-1).bitsize)
+    assert_equal(13, (2**12).bitsize)
+    assert_equal(13, (2**12+1).bitsize)
+  end
 end
Index: test/ruby/test_bignum.rb
===================================================================
--- test/ruby/test_bignum.rb	(revision 42210)
+++ test/ruby/test_bignum.rb	(working copy)
@@ -701,4 +701,23 @@ class TestBignum < Test::Unit::TestCase
     end
     assert_equal(T1024 ^ 10, T1024 ^ obj)
   end
+
+  def test_bitsize
+    assert_equal(10001, (-2**10000-1).bitsize)
+    assert_equal(10001, (-2**10000).bitsize)
+    assert_equal(10000, (-2**10000+1).bitsize)
+    assert_equal(10000, (2**10000-1).bitsize)
+    assert_equal(10001, (2**10000).bitsize)
+    assert_equal(10001, (2**10000+1).bitsize)
+
+    1.upto(1000) {|i|
+      n = 2**i
+      assert_equal(i+1, (-n-1).bitsize)
+      assert_equal(i+1, (-n).bitsize)
+      assert_equal(i,   (-n+1).bitsize)
+      assert_equal(i,   (n-1).bitsize)
+      assert_equal(i+1, (n).bitsize)
+      assert_equal(i+1, (n+1).bitsize)
+    }
+  end
 end
