Jump to content

Bit combination problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
Okay, so i have 52 bits and i need to generate all possible combination of bits, using only n bits at the time. The sequence is not important/duplicates must be avoided ie 1 3 5 and 3 5 1 is the same(as in any bit sequence). any ideas?
It's for a card game simulator.

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Check out my code over there

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Would it be feasible to use Ruby's combination method? Class: Array [ruby-doc.org]


You'll have to do a test to remove repeating values with this method though.

*Edited changed repeat_combination to combination.

Edited by lethalwire, 20 June 2011 - 09:30 AM.


#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
I found a very useful algorithm from the internet:

public static int numberOfSetBits(int i){
        i = i - ((i >> 1) & 0x55555555);
        i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
        return ((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
No idea how it works, but it's fast as hell. My computer checks every integer for n-bits from 0 to max_integer almost instantly. That's enough for me.
Just need to convert it for long type now.

Never mind, i'm reinventing the wheel, java has bitcount built in in the Long class

Edited by Sinipull, 21 June 2011 - 03:50 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users