added: #parity
authorClaus Gittinger <cg@exept.de>
Mon, 09 Jan 2012 17:09:54 +0100
changeset 13905 7eb024efb58a
parent 13904 1aacd72f4414
child 13906 5543bafd72a9
added: #parity
Integer.st
--- a/Integer.st	Mon Jan 09 16:55:31 2012 +0100
+++ b/Integer.st	Mon Jan 09 17:09:54 2012 +0100
@@ -4042,8 +4042,30 @@
      36 nextPrime
      3456737 nextPrime
     "
+!
+
+parity
+    "false if an even number of bits are set, true otherwise.
+     (i.e. true for odd parity)
+     Undefined for negative values (smalltalk does not require the machine to use 2's complement)"
+
+    ^ self bitCount odd
+
+    "
+     1 parity    
+     0 parity    
+     33 parity   
+     6 parity    
+
+     1 to:1000000 do:[:n |
+        self assert:(n parity = ((n printStringRadix:2) occurrencesOf:$1) odd)
+     ]
+    "
+
+    "Created: / 09-01-2012 / 17:01:53 / cg"
 ! !
 
+
 !Integer methodsFor:'special modulu arithmetic'!
 
 add_32:anInteger
@@ -4730,11 +4752,11 @@
 !Integer class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.256 2011-11-04 16:18:18 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.257 2012-01-09 16:09:54 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.256 2011-11-04 16:18:18 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Integer.st,v 1.257 2012-01-09 16:09:54 cg Exp $'
 ! !
 
 Integer initialize!