HP Forums

Full Version: Prime Program number of set bits
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hello,

I thought I'd share a simple program to determine the number of set bits in a base ten number. This is useful to me as I need to group numbers based on the number of ones in them for the Quine-McCluskey method of reducing Boolean functions. Although one can do 4 bit numbers in one's head easily, its nice to double check quickly and if a 5 bit number is needed it could be more helpful then. I may continue with the idea and try to program the whole method. Anyway, it was fun to do.

EXPORT numSetBits(n)
// Oct 2013
// return the number of ones in n
BEGIN
LOCAL i := 0; //index of ones
LOCAL defB := Base; //save default base
LOCAL binB := 0; //binary base
LOCAL bVal := 0; //conversion to binary

Base := binB;
bVal := R→B(n);

WHILE bVal≠0 DO
i:=i+1;
bVal:= bVal AND bVal-#1b;
END;

//set default base to original val
Base := defB;

RETURN i;
END;

Note:
I don't know how to format the following text here ...

"→" is actually "->" as in R->B(n);

"≠" is actually the "does not equal" symbol. (bVal!=0 doesn't work either one must use the Prime key it seems)

Enter a number onto the stack and execute the program and it will return the number of ones in the binary value.

15
ENTER
numSetBits()
ENTER
4
6
ENTER
numSetBits()
ENTER
2

#15d = #1111b ... 4 ones

#6d = #110b ... 2 ones

If anyone sees some improvements I could make, please let me know as I'm new to Prime programming.


Edited: 22 Oct 2013, 1:53 a.m.

Can't help with programming, but here are two ideas regarding counting how many 1's are in the binary form of an integer x.

Method 1: hamdist(x,0)

This only works for small numbers, < 2^30, roughly 9 decimal digits. But it's a single fast command, which is cool.

Method 2: SigmaLIST(convert(x,base,2))

This only works in CAS. Replace "Sigma" with the Sigma character of course, and be sure to type "convert" in lowercase or it won't work (unfortunately the Toolbox Catalog echoes it in uppercase). This method works for any exact CAS integer that Prime can handle (7999 bits, roughly 2048 decimal digits). Using this method, the emulator takes 0.83 seconds to count all 7998 1's in 2^7998-1 on my laptop.

-Joe-

I really like hamming distance. Who would of thought? I need to learn more about the function available on the calc. Thanks!

There's a very fast algorithm for counting the number of bits. Suppose you have a 32 bit binary number n where each 16-bit half contains the number of bits that your starting number had in each half. You can add these two numbers together with:

  tmp := BITSR(n,16);
n := BITAND(n, #0000FFFFh);
tmp := BITAND(tmp, #0000FFFFh);
n := n + tmp;
return n;

In a similar way, if n contains 4 fields of 8 bits each, you can add combine adjacent fields to form two 16 bit fields like this:

  tmp := BITSR(n, 8);
n := BITAND(n, #00FF00FFh);
tmp := BITAND(tmp, #00FF00FFh);
n := n + tmp;

The idea can be extended all the way down to 1-bit fields. So the code to count the bits is:

EXPORT numsetbits(n)
BEGIN
LOCAL tmp;

tmp := BITSR(n);

n := BITAND(n, #55555555h);
tmp := BITAND(tmp, #55555555h);
n := n + tmp;

// n is now broken into fields of 2 bits
// each. Each field contains the number of
// bits that were set in it.

tmp := BITSR(n, 2);
n := BITAND(n, #33333333h);
tmp := BITAND(tmp, #33333333h);
n := n + tmp;

// now each field is 4 bits wide
tmp := BITSR(n, 4);
n := BITAND(n, #0F0F0F0Fh);
tmp := BITAND(tmp, #0F0F0F0Fh);
n := n + tmp;

// Fields are now 8 bits

tmp := BITSR(n, 8);
n := BITAND(n, #00FF00FFh);
tmp := BITAND(tmp, #00FF00FFh);
n := n + tmp;

tmp := BITSR(n,16);
n := BITAND(n, #0000FFFFh);
tmp := BITAND(tmp, #0000FFFFh);
n := n + tmp;
return n;
END;