[squeak-dev] The Trunk: Kernel-nice.665.mcz

commits at source.squeak.org commits at source.squeak.org
Sun Jan 22 17:14:17 UTC 2012


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

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

Name: Kernel-nice.665
Author: nice
Time: 22 January 2012, 6:13:29.686 pm
UUID: 3122f4bd-6c16-4919-9b90-47080e9365cb
Ancestors: Kernel-nice.664, Kernel-nice.644

merge Kernel-nice.644

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

=============== Diff against Kernel-nice.664 ===============

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)"!




More information about the Squeak-dev mailing list