解决方案 »
- Byte问题
- 我Java中用SQL语句删除access数据库的记录,为什么用access打开时记录还在?
- 用java编了个小的计算器程序,但运行后无法输入数值,显示不出输入的数字,望好心人帮帮忙,谢谢了!!!
- 请教一个未能理解的问题.请求帮助.急急急急急急急急急!!十成火急,炮药火急!!!!
- 如何在图片上插入透明编辑框?
- 读懂英文原版教材需要背多少单词?
- **********<b>谁有JAVA的案例</b>*******
- 应该非常容易的问题!不管你懂不懂进来看看,大家交流交流也好!
- 怎么分别显示浮点数的整数部分和小书部分?
- 那位知道JBuilder中的tomcat与apache是什么关系?
- 面向对象的问题!牛人请进!!!!!!!!!!!!!!!!!
- 关于正则表达是的问题?
int a=23;
int result[31]={0};
int test=1;
int pos=0; for(int i=0;i<=30;i++)
{
if((a&test)!=0)
{
result[pos]=test;
pos++;
}
test=test<<1;
}
int number = 1025;
int degree = 0;//二进制中值为1的个数
for(int i=0;i<32;i++){
if(((1<<i)&number)!=0){
degree++;
}
}
int[] array = new int[degree];
int index = 0;
for(int i=0;i<32;i++){
if(((1<<i)&number)!=0){
array[index++]=(1<<i);
}
}
System.out.println(degree);
System.out.println(Arrays.toString(array));
}
List<Integer> list = new ArrayList<Integer>();
int idx = 2;
while(ind != 0){
int x =idx/2;
if(ind % idx != 0 ){
list.add(x);
ind -= x;
}else if(ind / idx == 1){
list.add(idx);
ind = 0;
break;
}
idx *= 2;
}
Integer[] a = new Integer[0];
a=list.toArray(a);
return a;
}
谢谢楼上两位,因为你们那样写的话,会多操作N多次,于是我从你们相反的方向出去最后算出写出来了,给大家分享一下
if (ind < 0) { // ind <= 0??
throw new IllegalArgumentException();
}
int[] array = new int[31];
int count = 0;
for (int i = 1; i > 0; i <<= 1) {
if ((ind & i) != 0) {
array[count++] = i;
}
}
int[] result = new int[count];
System.arraycopy(array, 0, result, 0, count);
return result;
}
public static int[] intToIntegerList(int ind) {
if (ind < 0) { // ind <= 0??
throw new IllegalArgumentException();
}
int[] array = new int[Integer.bitCount(ind)];
int count = 0;
for (int i = 1; i > 0 && count < array.length; i <<= 1) {
if ((ind & i) != 0) {
array[count++] = i;
}
}
return array;
}