Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!.POSTED!not-for-mail From: Willem Newsgroups: comp.theory,comp.programming,sci.math Subject: Re: n bits on of m bits Date: Thu, 22 Dec 2011 15:58:19 +0000 (UTC) Organization: Stack Usenet News Service Lines: 41 Message-ID: References: <4344a25e-7fd5-43c5-b43a-a004c9f5f2ad@y7g2000vbe.googlegroups.com> <99d65c5a-7678-4ecc-8b10-b3f475947211@d17g2000prl.googlegroups.com> NNTP-Posting-Host: toad.stack.nl Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: mud.stack.nl 1324569499 28626 2001:610:1108:5010::135 (22 Dec 2011 15:58:19 GMT) X-Complaints-To: abuse@stack.nl NNTP-Posting-Date: Thu, 22 Dec 2011 15:58:19 +0000 (UTC) User-Agent: slrn/0.9.9p1 (FreeBSD) Xref: x330-a1.tempe.blueboxinc.net comp.theory:1502 comp.programming:1135 Robin Vowels wrote: ) On Dec 18, 3:05?am, Andrew Tomazos wrote: )> I want to loop over every number from 0 to (2^m-1) inclusive whos )> binary representation contains exactly n bits. )> )> ie )> )> for (int i = 0; i < (1< { )> ? ? int bits = 0; )> ? ? for (int j = 0; j < m; j++) )> ? ? ? ? if (i & (1 << j)) )> ? ? ? ? ? ? bits++; )> ? ? if (bits == n) )> ? ? ? ? do_something_with(i); )> )> } )> )> Clearly this is terribly inefficient. ?I'm sure there is a much faster )> way (perhaps by enumerating with mod/div somehow each continous )> sequence of 1s and continuous sequence of 0'x) but I can't seem to see )> it. ) ) It is unclear what you want. Read the code then. It does what the OP wants, it's just very slow. ) Or do you mean n one-bits (as someone has assumed)? Not assumed, inferred. And not someone, everyone (except you). HTH, HAND. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT