标签:rate ati 返回 运算 循环 lin amp ica tput
2017/3/14 15:36:44
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
publicclassSolution{
publicint findComplement(int num){
num =~num;
int flag =31;
while((1<<flag & num)!=0){
--flag;
}
int comparer =1<<flag;
comparer |= comparer>>1;
comparer |= comparer>>2;
comparer |= comparer>>4;
comparer |= comparer>>8;
comparer |= comparer>>16;
return comparer & num ;
}
}
publicstaticint highestOneBit(int i){
// HD, Figure 3-1
i |=(i >>1);
i |=(i >>2);
i |=(i >>4);
i |=(i >>8);
i |=(i >>16);
return i -(i >>>1);
}
publicstaticint lowestOneBit(int i){
// HD, Section 2-1
return i &-i;
}
publicclassSolution{
publicint findComplement(int num){
return~num &((Integer.highestOneBit(num)<<1)-1);
}
}
publicclassSolution{
publicint findComplement(int num){
return~num &(Integer.highestOneBit(num)-1);
}
}
标签:rate ati 返回 运算 循环 lin amp ica tput
原文地址:http://www.cnblogs.com/flyfatty/p/6624792.html