On 13 Aug, 17:05, Chonku <[email protected]> wrote:
> Start with number 1. It will have a binary representation of 00...1 (Total
> of n-bits)
> Keeping adding 1 to it until you reach a number with all 1's in its binary
> representation.
>
Looks correct to me,
here is a small implementation
========================
<code>
#include <stdio.h>
int len;
void to_binary(int n)
{
int i = len - 1;
for(; i >= 0; i--) {
(n & 1<<i) ? printf(" 1"):printf(" 0");
}
printf("\n");
}
void generate_bits(int n)
{
if(n == 0) return;
generate_bits(n-1);
to_binary(n);
}
int main(void)
{
scanf("%d", &len);
generate_bits((1<<len) - 1);
}
</code>
========================
>
>
> On Thu, Aug 12, 2010 at 2:00 PM, Raj N <[email protected]> wrote:
> > Hi,
> > Can someone gimme the code to generate all possible bit strings of
> > length n recursively ?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Algorithm Geeks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<algogeeks%2bunsubscr...@googlegroups
> > .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/algogeeks?hl=en.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.