Integer.st
branchjv
changeset 17844 29a1536dffe4
parent 17841 7abcc4aef871
child 17845 7e0cfaac936d
--- a/Integer.st	Thu Jun 09 16:28:45 2011 +0100
+++ b/Integer.st	Thu Jun 23 16:55:10 2011 +0100
@@ -1139,7 +1139,6 @@
     ^ self == Integer
 ! !
 
-
 !Integer methodsFor:'Compatibility-Dolphin'!
 
 & aNumber
@@ -3918,30 +3917,8 @@
      100000 isPowerOf:10
      100001 isPowerOf:10
  "
-!
-
-isPowerOfTwo
-    "return true, if the receiver is a power of 2"
-
-"/ mhmh: how about the following
-"/    self == 0 ifTrue:[^ false].
-
-    ^ (self bitAnd:(self - 1)) == 0
-
-    "
-     0 isPowerOfTwo
-     1 isPowerOfTwo
-     2 isPowerOfTwo
-     3 isPowerOfTwo
-     4 isPowerOfTwo
-     16r8000000000000000 isPowerOfTwo
-     16r8000000000000001 isPowerOfTwo
-    "
-
-    "Modified: 15.10.1997 / 18:43:49 / cg"
 ! !
 
-
 !Integer methodsFor:'special access'!
 
 exponent
@@ -4387,6 +4364,32 @@
     ^ true
 !
 
+isPowerOfTwo
+    "return true, if the receiver is a power of 2"
+
+    "redefined, because the hacker's algorithm in smallInt is much slower for large numbers"
+
+    |maxBytes "{ Class: SmallInteger }"|
+
+    maxBytes := self digitLength.
+    (self digitAt:maxBytes) isPowerOfTwo ifFalse:[^ false].
+    1 to:maxBytes-1 do:[:byteIndex |
+        (self digitAt:byteIndex) ~~ 0 ifTrue:[^ false].
+    ].
+    ^ true
+
+    "
+     10000 factorial isPowerOfTwo  
+     |n| n := 10000 factorial. Time millisecondsToRun:[1000 timesRepeat:[ n isPowerOfTwo]] 
+    "
+    "
+     (2 raisedTo:10000) isPowerOfTwo  
+     |n| n := (2 raisedTo:10000). Time millisecondsToRun:[1000 timesRepeat:[ n isPowerOfTwo]] 
+    "
+
+    "Modified: / 20-06-2011 / 12:43:05 / cg"
+!
+
 isPrime
     "return true if I am a prime Number.
      This is a q&d hack, which may need optimization if heavily used."
@@ -4720,15 +4723,15 @@
 !Integer class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Integer.st 10643 2011-06-08 21:53:07Z vranyj1 $'
+    ^ '$Id: Integer.st 10648 2011-06-23 15:55:10Z vranyj1 $'
 !
 
 version_CVS
-    ^ 'Header: /cvs/stx/stx/libbasic/Integer.st,v 1.252 2011/05/13 13:58:36 stefan Exp '
+    ^ 'Header: /cvs/stx/stx/libbasic/Integer.st,v 1.253 2011/06/20 10:43:28 cg Exp '
 !
 
 version_SVN
-    ^ '$Id: Integer.st 10643 2011-06-08 21:53:07Z vranyj1 $'
+    ^ '$Id: Integer.st 10648 2011-06-23 15:55:10Z vranyj1 $'
 ! !
 
 Integer initialize!
@@ -4742,3 +4745,4 @@
 
 
 
+