向后处理Bitwise操作

时间:2014-02-07 04:35:35

标签: java bitwise-operators

我需要知道如何向后执行此操作,以便它适用于所有选项。如果我从shift开始,我可以获得基数,那么我将如何从基数开始转换。

int shift = 4; //3 will give octal base 4 will give Hex base 1 will give binary base
int radix = 1 << shift; // this comes out as 16

所以就像我上面所说的那样,我怎么会这样做呢?

int radix = 16;
int shift =(some operation);

如果基数为8,2或16

,也可以使其工作

2 个答案:

答案 0 :(得分:1)

Integer课程中有很多有用的方法。尝试

int shift = Integer.numberOfTrailingZeros(radix)

答案 1 :(得分:0)

尝试使用此方法,

public static int radixToShift(int radix){
    int shift = 0;
    for(int i=0;radix >> i >1;i++){
        shift++;
    }
    return shift;
}

代码

int shift = 4; 
int radix = 1 << shift;
System.out.println("Radix :: "+radix);
System.out.println("Shift :: "+radixToShift(radix));

输出

Radix :: 16
Shift :: 4