[pypy-svn] r77514 - in pypy/branch/fast-forward/pypy/objspace/std: . test

agaynor at codespeak.net agaynor at codespeak.net
Fri Oct 1 00:41:43 CEST 2010


Author: agaynor
Date: Fri Oct  1 00:41:41 2010
New Revision: 77514

Modified:
   pypy/branch/fast-forward/pypy/objspace/std/inttype.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py
Log:
Added int.bit_length.

Modified: pypy/branch/fast-forward/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/inttype.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/inttype.py	Fri Oct  1 00:41:41 2010
@@ -15,6 +15,18 @@
 def int_conjugate__ANY(space, w_int):
     return space.pos(w_int)
 
+int_bit_length = SMM("bit_length", 1, doc="int.bit_length() -> int\n\nNumber of bits necessary to represent self in binary.\n>>> bin(37)\n'0b100101'\n>>> (37).bit_length()\n6")
+
+def int_bit_length__ANY(space, w_int):
+    val = space.int_w(w_int)
+    if val < 0:
+        val = -val
+    bits = 0
+    while val:
+        bits += 1
+        val >>= 1
+    return space.wrap(bits)
+
 register_all(vars(), globals())
 
 

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py	Fri Oct  1 00:41:41 2010
@@ -437,6 +437,18 @@
         # __eq__ & the others.
         assert 1 .__cmp__
         assert int .__cmp__
+    
+    def test_bit_length(self):
+        for val, bits in [
+            (0, 0),
+            (1, 1),
+            (10, 4),
+            (150, 8),
+            (-1, 1),
+            (-10, 4),
+            (-150, 8),
+        ]:
+            assert val.bit_length() == bits
 
 
 class AppTestIntOptimizedAdd(AppTestInt):



More information about the Pypy-commit mailing list