2014-02-07 40 views
1

我需要知道如何反向操作,以便它適用於所有選項。如果我從輪班開始,我可以得到基數,那麼我將如何從基數開始轉換。向後工作按位操作

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 

所以,正如我前面如何將我得到這個去otherway

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

還要使這項工作,如果基數爲8,2或16

+0

你想要得到的'shift'給出一個基數? –

+0

是的,這是正確的 – user3282345

+1

右移1,直到基數爲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 
1

有許多有用的說方法在Integer類。嘗試

int shift = Integer.numberOfTrailingZeros(radix)