```Date: Mon, 25 Jun 2001 15:19:14 -0700 Reply-To: "Huang, Ya" Sender: "SAS(r) Discussion" From: "Huang, Ya" Subject: Re: Count the '1's in a binary number? Comments: To: "Droogendyk, Harry" Content-Type: multipart/alternative; Harry, Thank you very much! Ya Huang > -----Original Message----- > From: Droogendyk, Harry [mailto:Harry.Droogendyk@CIBC.COM] > Sent: Monday, June 25, 2001 2:37 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Re: Count the '1's in a binary number? > > > Not one line, but it does as you want. > > data _null_; > > number = 13; > power = 9999999; > cnt = 0; > do while ( number > 0 and power > 0); > cnt ++ 1; > power = int(log2(number)); > number = number - ( 2 ** power ); > end; > put cnt=; > run; > > -----Original Message----- > From: Huang, Ya [mailto:ya.huang@AGOURON.COM] > Sent: June 25, 2001 5:15 PM > To: SAS-L@LISTSERV.UGA.EDU > Subject: Count the '1's in a binary number? > > > > Hi there, > > 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. My instinct says that should be not very > difficult, > but I can't figure it out at the moment. > > Thanks > > Ya Huang > [text/html] ```

Back to: Top of message | Previous page | Main SAS-L page