Date: Mon, 25 Jun 2001 23:03:43 GMT
Reply-To: Jens Manfredson <HFUOQUCSVTYY@SPAMMOTEL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jens Manfredson <HFUOQUCSVTYY@SPAMMOTEL.COM>
Subject: Re: Count the '1's in a binary number?
Content-Type: text/plain; charset="iso-8859-1"
> Suppose I have a positive integer number and I want to count the number of
> '1's in the number presented with a binary format, for example, 13 is
> '1101' in binary, so the number of '1's = 3. I wonder if there is a way
> I can count it without first converting to character string then use
> compress() etc.? In other word, I need a pure mathematical algorithm, better
> to be a one line solution.
Normally you would do it with a shift operation and a mask operation.
Shift 1 right, mask with 0000001 and add.
Jens
|