Hello All... Though I've been using Squeak for several years now, and have added a few code snippets to wiki pages, I've yet to add any real code to the standard image. http://minnow.cc.gatech.edu/squeak/398 indicates that this list is the proper forum for enhancement requests, so here it is... I keep running into the need for a population count method for integers. This essentially tells you how many bits are set in a given int. It's useful for various cryptanalysis and integer coding tasks. So I figured that it might be of use to people other than myself. I'm including a changeset with the method as an attachment. Here's the code and an example of how it could be used:
File in the attached changeset or use copy and paste to add this method for class Integer...
popCount "Answers the number of bits set in an integer." | t1 t2 | t1 _ self. t2 _ 0. [t1 == 0] whileFalse: [t1 _ t1 bitAnd: t1 - 1. t2 _ t2 + 1]. ^ t2
And here's some code to exercise the method. Hilight this and print it.
| a b | a _ #( 2 3 5 7 11 13 17 19 23 29). b _ 0. a do: [ :current | b _ b + ( current factorial ). ]. b popCount.
squeak-dev@lists.squeakfoundation.org