The Trunk: Kernel-nice.644.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Kernel-nice.644.mcz

commits-2
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.644.mcz

==================== Summary ====================

Name: Kernel-nice.644
Author: nice
Time: 19 October 2011, 10:26:06.268 pm
UUID: 612f5a8e-66ec-4531-86ab-ebc494038a94
Ancestors: Kernel-nice.642

Add Integer>>bitCount to count the bits set (to 1) in an Integer.

=============== Diff against Kernel-nice.642 ===============

Item was added:
+ ----- Method: Integer>>bitCount (in category 'bit manipulation') -----
+ bitCount
+ "Count the number of bits set to 1 in self"
+
+ | bitCount |
+ self < 0 ifTrue: [self error: 'Cannot count bits of negative integers'].
+ bitCount := 0.
+ 1 to: self digitLength do: [:i |
+ bitCount := bitCount + (self digitAt: i) bitCountOfByte].
+ ^bitCount!

Item was added:
+ ----- Method: SmallInteger>>bitCountOfByte (in category 'bit manipulation') -----
+ bitCountOfByte
+ "Count the number of bits set to 1 in a byte."
+
+ ^#[0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 1 2 2 3 2 3 3 4 2 3 3 4 3 4 4 5 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 2 3 3 4 3 4 4 5 3 4 4 5 4 5 5 6 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 3 4 4 5 4 5 5 6 4 5 5 6 5 6 6 7 4 5 5 6 5 6 6 7 5 6 6 7 6 7 7 8] at: self + 1
+
+ "Evaluate this expression to form above byte array:
+ ((0 to: 255)
+ collect: [:i |
+ | bitCount n |
+ n := i.
+ bitCount := 0.
+ [n = 0]
+ whileFalse:
+ [bitCount := bitCount + 1.
+ n := n bitAnd: n - 1].
+ bitCount]
+ as: ByteArray)"!