| | |
| | | public class CommonUtil { |
| | | |
| | | //将数转换成二进制字符串并统计1的个数 |
| | | public static int getIntToBinary(int switchState){ |
| | | /*String binaryString=Integer.toBinaryString(switchState); |
| | | System.out.println(binaryString);*/ |
| | | public static int getIntToBinary(int switchState,int[] bit){ |
| | | int count=0; |
| | | while (switchState != 0){ |
| | | switchState = switchState & (switchState - 1); |
| | | /*int ss=switchState&(1<<bit); |
| | | while (ss != 0){ |
| | | ss = ss & (ss - 1); |
| | | count++; |
| | | }*/ |
| | | if(bit.length>0){ |
| | | for (int i=0;i<bit.length;i++){ |
| | | int ss=switchState&(1<<bit[i]); |
| | | if(ss>0){ |
| | | count++; |
| | | } |
| | | //System.out.println(count); |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | getIntToBinary(111111024); |
| | | System.out.println(getIntToBinary(15,new int[]{0,1,2,3})); |
| | | } |
| | | } |