in reply to Algorithm to convert combinations to bitstring

Misread :(

my %lookup = map { $_ => 1 << (ord($_) - ord('A')) } 'A'..'Z'; my $c = 'ABCHI'; my $bitmap = 0; $bitmap |= $lookup{$_} for $c =~ /(.)/g; print('A'..'Z', "\n"); print(scalar(reverse(sprintf("%26b", $bitmap))), "\n");

outputs

ABCDEFGHIJKLMNOPQRSTUVWXYZ 111000011

Update: Array version:

my @powers = map { 1 << $_ } 0..25; my $c = 'ABCHI'; my $bitmap = 0; $bitmap |= $powers[ord($_)-ord('A')] for $c =~ /(.)/g; print('A'..'Z', "\n"); print(scalar(reverse(sprintf("%26b", $bitmap))), "\n");

Update: Replaced += with |=. It makes it tolerant to repeated letters.

Replies are listed 'Best First'.
Re^2: Algorithm to convert combinations to bitstring
by Limbic~Region (Chancellor) on Oct 18, 2006 at 18:41 UTC
    ikegami,
    You will have to forgive me, but I have no idea how this solves the problem I asked. It could very well be that I have not done a good job of explaining what I want but in your example:
    N = 26 K = 25 C = 65,780
    I do not see how your output tells me that the combination representing 'ABCHI' which should be a single bit in a bitstring of 65,780 bits is present?

    Cheers - L~R