怎么办"&的indexOf QUOT;和" lastIndexOf"与位标志的整数? (获取发现指数的*功率*) [英] How to do "indexOf" and "lastIndexOf" on an integer with bit flags? (Get the *power* of the found index)

查看:237
本文介绍了怎么办"&的indexOf QUOT;和" lastIndexOf"与位标志的整数? (获取发现指数的*功率*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对我的<一个跟进href=\"http://stackoverflow.com/questions/23259194/why-is-1-plus-31-zeros-not-a-valid-integer-if-bit-flags-are-31-length-plus-one\">$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类有类似的 Integer.highestOneBit
对于迭代和位操作类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;

这篇关于怎么办&QUOT;&的indexOf QUOT;和&QUOT; lastIndexOf&QUOT;与位标志的整数? (获取发现指数的*功率*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆