Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取组成数字的二进制数列表_Java - Fatal编程技术网

Java 获取组成数字的二进制数列表

Java 获取组成数字的二进制数列表,java,Java,在Java中,有一个像0b1010这样的数字,我想得到一个“组成”这个数字的列表:0b1000和0b010,在这个示例中:每个位集一个数字 我不确定获得它的最佳解决方案。你有什么线索吗?使用AND操作逐个扫描位。这将告诉您是否在一个位置设置了位。(). 一旦确定设置了第i位,就组成一个字符串并打印它。伪代码: public static void PrintAllSubbitstrings(int number) { for(int i=0; i < 32; i++) //32 bi

在Java中,有一个像
0b1010
这样的数字,我想得到一个“组成”这个数字的列表:
0b1000
0b010
,在这个示例中:每个位集一个数字


我不确定获得它的最佳解决方案。你有什么线索吗?

使用AND操作逐个扫描位。这将告诉您是否在一个位置设置了位。(). 一旦确定设置了第i位,就组成一个字符串并打印它。伪代码:

public static void PrintAllSubbitstrings(int number)
{
   for(int i=0; i < 32; i++) //32 bits maximum for an int
   {
        if( number & (1 << i) != 0) //the i'th bit is set.
        {
            //Make up a bitstring with (i-1) zeroes to the right, then one 1 on the left
            String bitString = "1";
            for(int j=0; j < (i-1); j++) bitString += "0";
            System.out.println(bitString);
        }
   }
}
public static void PrintAllSubbitstrings(整数)
{
for(int i=0;i<32;i++)//int的最大值为32位
{

if(number&(1使用AND操作逐个扫描位。这将告诉您是否设置了某个位置的位。()。确定设置了第i位后,组成一个字符串并打印它。伪代码:

public static void PrintAllSubbitstrings(int number)
{
   for(int i=0; i < 32; i++) //32 bits maximum for an int
   {
        if( number & (1 << i) != 0) //the i'th bit is set.
        {
            //Make up a bitstring with (i-1) zeroes to the right, then one 1 on the left
            String bitString = "1";
            for(int j=0; j < (i-1); j++) bitString += "0";
            System.out.println(bitString);
        }
   }
}
public static void PrintAllSubbitstrings(整数)
{
for(int i=0;i<32;i++)//int的最大值为32位
{

如果(数字&(1这里有一个适合我的小测试

 public static void main(String[] args) {
   int num = 0b1010;
   int testNum = 0b1;
   while(testNum < num) {
       if((testNum & num) >0) {
           System.out.println(testNum + " Passes");
       } 
       testNum *= 2;
   }
}
publicstaticvoidmain(字符串[]args){
int num=0b1010;
int testNum=0b1;
while(testNum0){
System.out.println(testNum+“通过”);
} 
testNum*=2;
}
}

这里有一个对我有用的小测试

 public static void main(String[] args) {
   int num = 0b1010;
   int testNum = 0b1;
   while(testNum < num) {
       if((testNum & num) >0) {
           System.out.println(testNum + " Passes");
       } 
       testNum *= 2;
   }
}
publicstaticvoidmain(字符串[]args){
int num=0b1010;
int testNum=0b1;
while(testNum0){
System.out.println(testNum+“通过”);
} 
testNum*=2;
}
}
使用一个

如果你真的想把它们打印成二进制字符串,下面是对上述方法的一点改进:

long x = 0b101011;
char[] cs = new char[bs.length()];
Arrays.fill(cs, '0');

BitSet bs = BitSet.valueOf(new long[]{x});
for (int i = bs.nextSetBit(0); i >=0 ; i = bs.nextSetBit(i+1)) {
    cs[bs.length()-i-1] = '1';
    System.out.println(new String(cs));  // or whatever you want to do with this String
    cs[bs.length()-i-1] = '0';
}
输出:

1
2
8
32
000001
000010
001000
100000
用一把枪

如果你真的想把它们打印成二进制字符串,下面是对上述方法的一点改进:

long x = 0b101011;
char[] cs = new char[bs.length()];
Arrays.fill(cs, '0');

BitSet bs = BitSet.valueOf(new long[]{x});
for (int i = bs.nextSetBit(0); i >=0 ; i = bs.nextSetBit(i+1)) {
    cs[bs.length()-i-1] = '1';
    System.out.println(new String(cs));  // or whatever you want to do with this String
    cs[bs.length()-i-1] = '0';
}
输出:

1
2
8
32
000001
000010
001000
100000

使用位运算符检查是否在所需的值中设置。使用位运算符检查是否在所需的值中设置。您可以使用Integer.toBinaryString():)哦!你知道,我知道必须有某种方法可以做到这一点-到处查看
java.text
java.util.Formatter
并放弃了。哇!你可以使用Integer.tobinarysting():)哦!你知道,我知道必须有某种方法可以做到这一点-到处找
java.text
java.util.Formatter
然后放弃了。哇!