Java “怎么做?”;indexOf";及;lastIndexOf“;在具有位标志的整数上?(获取找到的索引的*幂*)
这是我的后续行动,我澄清了一些重大误解 我需要创建这些函数,以便在包含零个或多个标志的int中查找一个位标志:Java “怎么做?”;indexOf";及;lastIndexOf“;在具有位标志的整数上?(获取找到的索引的*幂*),java,bit-manipulation,Java,Bit Manipulation,这是我的后续行动,我澄清了一些重大误解 我需要创建这些函数,以便在包含零个或多个标志的int中查找一个位标志: BitBinaryUtil int twoExponentOfHighestOneBit(int all_flags) int twoExponentOfHighestOneBitAtMost(int all_flags, int twoExponentOfTwo_max0Thr30Incl) int twoExponentOfLowestOne
BitBinaryUtil
int twoExponentOfHighestOneBit(int all_flags)
int twoExponentOfHighestOneBitAtMost(int all_flags,
int twoExponentOfTwo_max0Thr30Incl)
int twoExponentOfLowestOneBit(int all_flags)
int twoExponentOfLowestOneBitAtLeast(int all_flags,
int twoExponentOfTwo_min0Thr30Incl)
它们大致类似于String.indexOf
和lastIndexOf
,只是它们返回找到的位的两个的指数。例如(全部31位)
我该怎么做呢?这是一个脑残。首先,我通过将int转换为长度为31的二进制字符串(据我所知,这是位标志的限制)来解决这个问题: 输出:
31-bits: 1000000000000000000000000000000, int: 1073741824
Highest one-bit is two-to-the-exponent-of: 30
Next-highest one-bit is two-to-the-exponent-of: -1
31-bits: 0000100000000000000000000000000, int: 67108864
Highest one-bit is two-to-the-exponent-of: 26
Next-highest one-bit is two-to-the-exponent-of: -1
31-bits: 0000000000000000000000000000010, int: 2
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: -1
31-bits: 0000000000000000000000000000000, int: 0
Highest one-bit is two-to-the-exponent-of: -1
31-bits: 0110000000000000000000000000000, int: 805306368
Highest one-bit is two-to-the-exponent-of: 29
Next-highest one-bit is two-to-the-exponent-of: 28
31-bits: 0000000000000000000000000000011, int: 3
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: 0
Testing: 31-bit binary=1000000000000000000000000000000, int=1073741824
Highest one-bit is two-to-the-exponent-of: 30
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 30
Testing: 31-bit binary=0000100000000000000000000000000, int=67108864
Highest one-bit is two-to-the-exponent-of: 26
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 26
Next-highest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0000000000000000000000000000010, int=2
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0000000000000000000000000000000, int=0
Highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0110000000000000000000000000000, int=805306368
Highest one-bit is two-to-the-exponent-of: 29
Next-highest one-bit is two-to-the-exponent-of: 28
Lowest one-bit is two-to-the-exponent-of: 28
Next-highest one-bit is two-to-the-exponent-of: 29
Testing: 31-bit binary=0000000000000000000000000000011, int=3
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: 0
Lowest one-bit is two-to-the-exponent-of: 0
Next-highest one-bit is two-to-the-exponent-of: 1
然后我通过位操作解决了这个问题:
/**
<P>Given a 31-bit binary-int, as a string (representing an int with zero or more bit-flags, where the most-significant-bit, the "sign bit", is removed), get the <I>inverted</I> index of the first one-bit (if the left-most bit is a 1, 31 is the result).</P>
<P>{@code java InvertedIndexOfHighestOneBitUsingInts}</P>
**/
public class InvertedIndexOfHighestOneBitUsingInts {
public static final int VALUE_OF_31ST_ONE_BIT = Integer.parseInt("1000000000000000000000000000000", 2);
public static final void main(String[] ignored) {
//One bit
test(VALUE_OF_31ST_ONE_BIT);
test(Integer.parseInt("0000100000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000010", 2));
test(Integer.parseInt("0000000000000000000000000000000", 2));
//Two
test(Integer.parseInt("0110000000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000011", 2));
}
private static final void test(int num) {
System.out.println("Testing: 31-bit binary=" + getIntAsZeroPadded31BitStringNoSign(num) + ", int=" + num);
int exponent = twoExponentOfHighestOneBit(num);
System.out.println(" Highest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent > 0) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfHighestOneBitAtMost(num, (exponent - 1)));
}
exponent = twoExponentOfLowestOneBit(num);
System.out.println(" Lowest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent != -1 && exponent < 30) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfLowestOneBitAtLeast(num, (exponent + 1)));
}
System.out.println();
}
完整代码:
/**
<P>Given a 31-bit binary-int, as a string (representing an int with zero or more bit-flags, where the most-significant-bit, the "sign bit", is removed), get the <I>inverted</I> index of the first one-bit (if the left-most bit is a 1, 31 is the result).</P>
<P>{@code java InvertedIndexOfHighestOneBitUsingBinaryStrings}</P>
**/
public class InvertedIndexOfHighestOneBitUsingBinaryStrings {
public static final void main(String[] ignored) {
//One bit
test("1000000000000000000000000000000"); //VALUE_OF_31ST_ONE_BIT
test("0000100000000000000000000000000");
test("0000000000000000000000000000010");
test("0000000000000000000000000000000");
//Two
test("0110000000000000000000000000000");
test("0000000000000000000000000000011");
}
private static final void test(String int_binStr) {
int i = Integer.parseInt(int_binStr, 2);
System.out.println("31-bits: " + int_binStr + ", int: " + i);
int exponent = getInvertedIndexHighestOneBit(int_binStr);
System.out.println(" Highest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent != -1) {
exponent = getInvertedIndexHighestOneBit(int_binStr, (exponent - 1));
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + exponent);
}
System.out.println();
}
public static final int getInvertedIndexHighestOneBit(String all31Bits_zeroPadded) {
return getInvertedIndexHighestOneBit(all31Bits_zeroPadded, 30);
}
public static final int getInvertedIndexHighestOneBit(String all31Bits_zeroPadded, int inverted_startIdx) {
int idxFirstOneBit = all31Bits_zeroPadded.indexOf("1", (30 - inverted_startIdx));
if(idxFirstOneBit == -1) {
return -1;
}
return (30 - idxFirstOneBit);
}
}
/**
给定一个31位二进制整数,作为字符串(表示具有零个或多个位标志的整数,其中最高有效位“符号位”被删除),获取前一位的反向索引(如果最左边的位是1,则结果为31)。
{@code java INVERSDINDEX HEHSONEBITUsingBinaryStrings}
**/
使用二进制字符串的HighstoneBit的公共类反相器索引{
公共静态最终void main(忽略字符串[]){
//一点点
测试(“10000000000000000000000000000000000”);//第31位的值
测试(“00001000000000000000”);
测试(“00000000000000000010”);
测试(“0000000000000000000000000”);
//两个
测试(“0110000000000000000000000”);
测试(“00000000000000000000000011”);
}
专用静态最终无效测试(字符串int_binStr){
inti=Integer.parseInt(int_binStr,2);
System.out.println(“31位:+int_binStr+”,int:+i);
int exponent=getInvertedIndexHighestOneBit(int_binStr);
System.out.println(“最高一位是:“+指数”的指数的两倍);
如果(指数!=-1){
指数=getInvertedIndexHighestOneBit(int_binStr,(指数-1));
System.out.println(“下一个最高的一位是:“+指数”的指数的2);
}
System.out.println();
}
公共静态最终int getInvertedIndexHighestOneBit(字符串全部31位加零){
返回getInvertedIndexHighestOneBit(所有31位加零,30位);
}
公共静态最终整型getInvertedIndexHighestOneBit(字符串全部31位加零,整型反向){
int idxFirstOneBit=所有31位加零的.indexOf(“1”,(30-反向的\u startIdx));
if(idxFirstOneBit==-1){
返回-1;
}
返回(30-idxFirstOneBit);
}
}
位:
/**
给定一个31位二进制整数,作为字符串(表示具有零个或多个位标志的整数,其中最高有效位“符号位”被删除),获取前一位的反向索引(如果最左边的位是1,则结果为31)。
{@code java INVERTDINDEXOFHIGHSTONEBITUSINGTS}
**/
黑石沥青混合料的公共级指数{
公共静态最终整型值为一位=Integer.parseInt(“10000000000000000000000000000000000”,2);
公共静态最终void main(忽略字符串[]){
//一点点
测试(一位的值);
测试(Integer.parseInt(“00001000000000000000”,2));
测试(Integer.parseInt(“00000000000000000000000010”,2));
测试(Integer.parseInt(“0000000000000000000000000”,2));
//两个
测试(Integer.parseInt(“0110000000000000000000000”,2));
测试(Integer.parseInt(“00000000000000000000000011”,2));
}
专用静态最终无效测试(int num){
System.out.println(“测试:31位二进制=“+getIntAsZeroPadded31BitStringNoSign(num)+”,int=“+num”);
int指数=比特的两个指数(num);
System.out.println(“最高一位是:“+指数”的指数的两倍);
如果(指数>0){
System.out.println(“下一个最高的一位是:”+TwoExponentof HehstoneBitAtmost(num,(exponent-1)))的指数的2);
}
指数=Westonebit的两个指数(num);
System.out.println(“最低一位是:“+指数”的指数的两倍);
如果(指数!=-1&&指数<30){
System.out.println(“下一个最高的一位是:”+TwoExponentOfWestoneBitatleast(num,(指数+1))的指数的2);
}
System.out.println();
}
公共静态最终int Two指数(int all_标志){
返回HehestoneBitAtmost的两个指数(所有_标志,30);
}
HighstoneBitAtmost的公共静态最终int Two指数(int all_标志,int Two指数of two最大值为0th30incl){
if(两个最大0th30incl的两个指数<0 | |两个最大0th30incl的两个指数>30){
抛出新的IllegalArgumentException(“Two-Exponentoftwo_max0th30incl”(“+Two-Exponentoftwo_max0th30incl+”)必须介于1和31之间,包括在内);
}
//第一位的索引:
int currentBitMask=一位>>的值(30-两个最大值的两个指数,包括30);
int指数=两个指数中的两个指数,最大值为0th30incl;
而(指数>=0){
//System.out.println(“high.currentBitMask=“+getIntAsZeroPadded31BitStringNoSign(currentBitMask)+”,exponent=“+exponent+”);
//System.out.println(“high.all_flags=“+getIntAsZeroPadded31BitStringNoSign(all_flags));
if((所有_标志和currentBitMask)==currentBitMask){
回归指数;
}
//System.out.println();
指数--;
currentBitMask>>=1;
}
返回-1;
}
公共静态最终int TwoExponentOfWestoneBit(int all_标志){
返回至少两个WestoneBit的指数(所有_标志,0);
}
公共静态最终int Two Exponent of WestoneBit至少(int all_标志,int Two Exponent of two_mino th30包括在内){
if(两个最小thr30incl的两个指数<0 | |两个最小th30incl的两个指数>30){
抛出新的IllegalArgumentException(“
/**
<P>Given a 31-bit binary-int, as a string (representing an int with zero or more bit-flags, where the most-significant-bit, the "sign bit", is removed), get the <I>inverted</I> index of the first one-bit (if the left-most bit is a 1, 31 is the result).</P>
<P>{@code java InvertedIndexOfHighestOneBitUsingInts}</P>
**/
public class InvertedIndexOfHighestOneBitUsingInts {
public static final int VALUE_OF_31ST_ONE_BIT = Integer.parseInt("1000000000000000000000000000000", 2);
public static final void main(String[] ignored) {
//One bit
test(VALUE_OF_31ST_ONE_BIT);
test(Integer.parseInt("0000100000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000010", 2));
test(Integer.parseInt("0000000000000000000000000000000", 2));
//Two
test(Integer.parseInt("0110000000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000011", 2));
}
private static final void test(int num) {
System.out.println("Testing: 31-bit binary=" + getIntAsZeroPadded31BitStringNoSign(num) + ", int=" + num);
int exponent = twoExponentOfHighestOneBit(num);
System.out.println(" Highest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent > 0) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfHighestOneBitAtMost(num, (exponent - 1)));
}
exponent = twoExponentOfLowestOneBit(num);
System.out.println(" Lowest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent != -1 && exponent < 30) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfLowestOneBitAtLeast(num, (exponent + 1)));
}
System.out.println();
}
public static final int twoExponentOfHighestOneBit(int all_flags) {
return twoExponentOfHighestOneBitAtMost(all_flags, 30);
}
public static final int twoExponentOfHighestOneBitAtMost(int all_flags, int twoExponentOfTwo_max0Thr30Incl) {
if(twoExponentOfTwo_max0Thr30Incl < 0 || twoExponentOfTwo_max0Thr30Incl > 30) {
throw new IllegalArgumentException("twoExponentOfTwo_max0Thr30Incl (" + twoExponentOfTwo_max0Thr30Incl + ") must be between 1 and 31, inclusive");
}
//Index of first one bit:
int currentBitMask = VALUE_OF_31ST_ONE_BIT >> (30 - twoExponentOfTwo_max0Thr30Incl);
int exponent = twoExponentOfTwo_max0Thr30Incl;
while(exponent >= 0) {
//System.out.println(" high.currentBitMask=" + getIntAsZeroPadded31BitStringNoSign(currentBitMask) + ", exponent=" + exponent + "");
//System.out.println(" high.all_flags= " + getIntAsZeroPadded31BitStringNoSign(all_flags));
if((all_flags & currentBitMask) == currentBitMask) {
return exponent;
}
//System.out.println();
exponent--;
currentBitMask >>= 1;
}
return -1;
}
public static final int twoExponentOfLowestOneBit(int all_flags) {
return twoExponentOfLowestOneBitAtLeast(all_flags, 0);
}
public static final int twoExponentOfLowestOneBitAtLeast(int all_flags, int twoExponentOfTwo_min0Thr30Incl) {
if(twoExponentOfTwo_min0Thr30Incl < 0 || twoExponentOfTwo_min0Thr30Incl > 30) {
throw new IllegalArgumentException("twoExponentOfTwo_min0Thr30Incl (" + twoExponentOfTwo_min0Thr30Incl + ") must be between 1 and 30, inclusive.");
}
//Index of first one bit:
int currentBitMask = 1 << twoExponentOfTwo_min0Thr30Incl;
int exponent = twoExponentOfTwo_min0Thr30Incl;
while(exponent <= 30) {
//System.out.println(" low.currentBitMask=" + getIntAsZeroPadded31BitStringNoSign(currentBitMask) + ", exponent=" + exponent + "");
//System.out.println(" low.all_flags= " + getIntAsZeroPadded31BitStringNoSign(all_flags));
if((all_flags & currentBitMask) == currentBitMask) {
return exponent;
}
//System.out.println();
exponent++;
currentBitMask <<= 1;
}
return -1;
}
public static final String getIntAsZeroPadded31BitStringNoSign(int num) {
return String.format("%32s", Integer.toBinaryString(num)).replace(' ', '0').substring(1, 32);
}
}
Testing: 31-bit binary=1000000000000000000000000000000, int=1073741824
Highest one-bit is two-to-the-exponent-of: 30
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 30
Testing: 31-bit binary=0000100000000000000000000000000, int=67108864
Highest one-bit is two-to-the-exponent-of: 26
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 26
Next-highest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0000000000000000000000000000010, int=2
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0000000000000000000000000000000, int=0
Highest one-bit is two-to-the-exponent-of: -1
Lowest one-bit is two-to-the-exponent-of: -1
Testing: 31-bit binary=0110000000000000000000000000000, int=805306368
Highest one-bit is two-to-the-exponent-of: 29
Next-highest one-bit is two-to-the-exponent-of: 28
Lowest one-bit is two-to-the-exponent-of: 28
Next-highest one-bit is two-to-the-exponent-of: 29
Testing: 31-bit binary=0000000000000000000000000000011, int=3
Highest one-bit is two-to-the-exponent-of: 1
Next-highest one-bit is two-to-the-exponent-of: 0
Lowest one-bit is two-to-the-exponent-of: 0
Next-highest one-bit is two-to-the-exponent-of: 1
/**
<P>Given a 31-bit binary-int, as a string (representing an int with zero or more bit-flags, where the most-significant-bit, the "sign bit", is removed), get the <I>inverted</I> index of the first one-bit (if the left-most bit is a 1, 31 is the result).</P>
<P>{@code java InvertedIndexOfHighestOneBitUsingBinaryStrings}</P>
**/
public class InvertedIndexOfHighestOneBitUsingBinaryStrings {
public static final void main(String[] ignored) {
//One bit
test("1000000000000000000000000000000"); //VALUE_OF_31ST_ONE_BIT
test("0000100000000000000000000000000");
test("0000000000000000000000000000010");
test("0000000000000000000000000000000");
//Two
test("0110000000000000000000000000000");
test("0000000000000000000000000000011");
}
private static final void test(String int_binStr) {
int i = Integer.parseInt(int_binStr, 2);
System.out.println("31-bits: " + int_binStr + ", int: " + i);
int exponent = getInvertedIndexHighestOneBit(int_binStr);
System.out.println(" Highest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent != -1) {
exponent = getInvertedIndexHighestOneBit(int_binStr, (exponent - 1));
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + exponent);
}
System.out.println();
}
public static final int getInvertedIndexHighestOneBit(String all31Bits_zeroPadded) {
return getInvertedIndexHighestOneBit(all31Bits_zeroPadded, 30);
}
public static final int getInvertedIndexHighestOneBit(String all31Bits_zeroPadded, int inverted_startIdx) {
int idxFirstOneBit = all31Bits_zeroPadded.indexOf("1", (30 - inverted_startIdx));
if(idxFirstOneBit == -1) {
return -1;
}
return (30 - idxFirstOneBit);
}
}
/**
<P>Given a 31-bit binary-int, as a string (representing an int with zero or more bit-flags, where the most-significant-bit, the "sign bit", is removed), get the <I>inverted</I> index of the first one-bit (if the left-most bit is a 1, 31 is the result).</P>
<P>{@code java InvertedIndexOfHighestOneBitUsingInts}</P>
**/
public class InvertedIndexOfHighestOneBitUsingInts {
public static final int VALUE_OF_31ST_ONE_BIT = Integer.parseInt("1000000000000000000000000000000", 2);
public static final void main(String[] ignored) {
//One bit
test(VALUE_OF_31ST_ONE_BIT);
test(Integer.parseInt("0000100000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000010", 2));
test(Integer.parseInt("0000000000000000000000000000000", 2));
//Two
test(Integer.parseInt("0110000000000000000000000000000", 2));
test(Integer.parseInt("0000000000000000000000000000011", 2));
}
private static final void test(int num) {
System.out.println("Testing: 31-bit binary=" + getIntAsZeroPadded31BitStringNoSign(num) + ", int=" + num);
int exponent = twoExponentOfHighestOneBit(num);
System.out.println(" Highest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent > 0) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfHighestOneBitAtMost(num, (exponent - 1)));
}
exponent = twoExponentOfLowestOneBit(num);
System.out.println(" Lowest one-bit is two-to-the-exponent-of: " + exponent);
if(exponent != -1 && exponent < 30) {
System.out.println(" Next-highest one-bit is two-to-the-exponent-of: " + twoExponentOfLowestOneBitAtLeast(num, (exponent + 1)));
}
System.out.println();
}
public static final int twoExponentOfHighestOneBit(int all_flags) {
return twoExponentOfHighestOneBitAtMost(all_flags, 30);
}
public static final int twoExponentOfHighestOneBitAtMost(int all_flags, int twoExponentOfTwo_max0Thr30Incl) {
if(twoExponentOfTwo_max0Thr30Incl < 0 || twoExponentOfTwo_max0Thr30Incl > 30) {
throw new IllegalArgumentException("twoExponentOfTwo_max0Thr30Incl (" + twoExponentOfTwo_max0Thr30Incl + ") must be between 1 and 31, inclusive");
}
//Index of first one bit:
int currentBitMask = VALUE_OF_31ST_ONE_BIT >> (30 - twoExponentOfTwo_max0Thr30Incl);
int exponent = twoExponentOfTwo_max0Thr30Incl;
while(exponent >= 0) {
//System.out.println(" high.currentBitMask=" + getIntAsZeroPadded31BitStringNoSign(currentBitMask) + ", exponent=" + exponent + "");
//System.out.println(" high.all_flags= " + getIntAsZeroPadded31BitStringNoSign(all_flags));
if((all_flags & currentBitMask) == currentBitMask) {
return exponent;
}
//System.out.println();
exponent--;
currentBitMask >>= 1;
}
return -1;
}
public static final int twoExponentOfLowestOneBit(int all_flags) {
return twoExponentOfLowestOneBitAtLeast(all_flags, 0);
}
public static final int twoExponentOfLowestOneBitAtLeast(int all_flags, int twoExponentOfTwo_min0Thr30Incl) {
if(twoExponentOfTwo_min0Thr30Incl < 0 || twoExponentOfTwo_min0Thr30Incl > 30) {
throw new IllegalArgumentException("twoExponentOfTwo_min0Thr30Incl (" + twoExponentOfTwo_min0Thr30Incl + ") must be between 1 and 30, inclusive.");
}
//Index of first one bit:
int currentBitMask = 1 << twoExponentOfTwo_min0Thr30Incl;
int exponent = twoExponentOfTwo_min0Thr30Incl;
while(exponent <= 30) {
//System.out.println(" low.currentBitMask=" + getIntAsZeroPadded31BitStringNoSign(currentBitMask) + ", exponent=" + exponent + "");
//System.out.println(" low.all_flags= " + getIntAsZeroPadded31BitStringNoSign(all_flags));
if((all_flags & currentBitMask) == currentBitMask) {
return exponent;
}
//System.out.println();
exponent++;
currentBitMask <<= 1;
}
return -1;
}
public static final String getIntAsZeroPadded31BitStringNoSign(int num) {
return String.format("%32s", Integer.toBinaryString(num)).replace(' ', '0').substring(1, 32);
}
}
int n = Integer.numberOfTrailingZeros(Integer.highestOneBit(x)) + 1;
public class BinaryUtilTest {
@Test
public void testName() throws Exception {
int given = 17;
String givenString = Integer.toBinaryString(given);
System.out.println(givenString);
System.out.println("twoExponentOfHighestOneBit: " + (givenString.length()-givenString.indexOf('1')-1));
System.out.println("twoExponentOfLowestOneBit: " + (givenString.length()-givenString.lastIndexOf('1')-1));
}
}
10001
twoExponentOfHighestOneBit: 4
twoExponentOfLowestOneBit: 0