比如说15个数,
二进制1111
怎么判断左边第一个数是1还是0
用位运算?
[code="java"]public class Test {
public static void main(String args[]) {
int pow = 5;
for(byte i = 0;i < (int)Math.pow(2, pow);i ++)
{
// 8 is 1000, so we use (i&8) to know if the most left is 1 or 0
if((i & (int)Math.pow(2, pow - 1)) > 0)
{
System.out.println("Left of Binary " + i + " is " + 1);
}
else
{
System.out.println("Left of Binary " + i + " is " + 0);
}
}
}
}[/code]
把他专成字符串
之后看他startWith0或者1不就行了