Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Algorithm to convert combinations to bitstring

by ikegami (Patriarch)
on Oct 18, 2006 at 18:30 UTC ( [id://579163]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://579163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-19 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found