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

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Thu Oct 20 14:53:16 UTC 2011

```2011/10/20 David T. Lewis <lewis at mail.msen.com>:
> 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:.
>

Our integers are unbounded because arbitrarily large. So the numbers
of bits set to 1 is infinite for a negative integer...
So #bitCount will fail for the same reason as #highBit.

Or we could answer the bitCountOfMagnitude ?
Or count the bit set to zero for a negative number ? (and let highBit
answer position of higher 0)...

Nicolas

> 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
>
>
>

```