问题描述
这是对我的$p$pvious在,在那里我清理了一些大的误解位标志的问题。
This is a follow up on my previous question on bit flags, where I cleared up some big misconceptions.
我需要创建这些功能以发现含有零个或多个标志单个位标志在一个int:
I need to create these functions to find a single bit-flag in an int containing zero or more flags:
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位)
Which are roughly similar to String.indexOf
and lastIndexOf
, except they return the exponent of two of the found-bit. For example (all 31 bits)
twoExponentOfHighestOneBit:
1000000000000000000000000000000 --> 30 (2^30=1073741824)
0000000000000000000000000000001 --> 0 (2^0=1)
0000000000000100000000000000001 --> 18
0000000000000000000000000000000 --> -1
twoExponentOfLowestOneBit:
1000000000000000000000000000000 --> 30
0000000000000000000000000000001 --> 0
0000000000000100000000000000001 --> 0
我怎样才能做到这一点?
How can I do this?
推荐答案
Integer类有类似的。
对于迭代和位操作类BitSet中提供了很好的功能了。
但整数就行了。
The class Integer has many bit functions like Integer.highestOneBit.For iteration and bit manipulation the class BitSet offers nice features too.But Integer will do.
是这样的:
int n = Integer.numberOfTrailingZeros(Integer.highestOneBit(x)) + 1;
这篇关于怎么办"&的indexOf QUOT;和" lastIndexOf"与位标志的整数? (获取发现指数的*功率*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!