[squeak-dev] The Inbox: Kernel-nice.644.mcz

David T. Lewis lewis at mail.msen.com
Thu Oct 20 13:42:29 UTC 2011


On Wed, Oct 19, 2011 at 08:26:18PM +0000, commits at source.squeak.org wrote:
> Nicolas Cellier uploaded a new version of Kernel to project The Inbox:
> http://source.squeak.org/inbox/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.
> 

I like this.

But if #bitAt: is defined for all integers, then for consistency
#bitCount should work for negative integers also. Integers don't
really have bits unless you are thinking in terms of the underlying
representation, which is certain to be twos complement in any use
case that I can imagine. So counting bits in a negative integer
should work in the same manner implied by #bitAt:.

I am surprised that a method like this does not already exist. But
the method finder does not seem to find #bitCount either, so I wonder
if some equivalent method already exists somewhere?

BTW, tinkerers may enjoy playing with www.squeaksource.com/TwosComplement,
which demonstrates bit twiddling in twos complement register arithmetic:

  16r0505 bitCount ==> 4
  (16r0505 asRegister bits select: [:bit | bit value]) size ==> 4

Dave




More information about the Squeak-dev mailing list