Skip to content

Instantly share code, notes, and snippets.

@pcyu16
Created April 4, 2010 23:02
Show Gist options
  • Select an option

  • Save pcyu16/355785 to your computer and use it in GitHub Desktop.

Select an option

Save pcyu16/355785 to your computer and use it in GitHub Desktop.
all permutation example, using bit mask checking
public class code
{
public static int pow2(int power)
{
int ret = 1;
for(int t=0;t<power;t++)
ret <<= 1;
return ret;
}
public static void main(String [] args)
{
final int total = 3;
final int [] num = {3,5,11};
boolean [] state = new boolean[total];
for(int x=1;x<code.pow2(total);x++)
{
for(int i=0;i<total;i++)
{
// if the ith bit of x is not 0
if( ( ( 1 << i ) & x ) != 0 )
state[i] = true;
else
state[i] = false;
}
String output = new String();
for(int i=0;i<total;i++)
{
if( state[i] == true )
output = output + num[i] + " ";
}
System.out.println(output);
}
System.exit(0);
}
}
@pcyu16
Copy link
Copy Markdown
Author

pcyu16 commented Apr 4, 2010

all permutation example
using bit mask checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment