利用位运算符有哪些好方法?
我经常遇到使用按位运算符来完成快速、简单且优雅的事情的人。我想学习一些有用的技巧。最有用的位运算符有哪些情况?
I constantly meet people using bitwise operators to do quick, easy and elegant things. I'd like to learn some useful tricks. What are some of the most useful bitwise operator cases?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
常数时间 2 次幂:
Constant time 2-exponentiation:
虽然我总体上同意 Michael McGowan 的观点,但位调整技巧在某些情况下可能非常有用,例如在对不具备所有常用指令的嵌入式硬件进行编程时。在将程序编码到定理证明器(例如 SMT 求解器)中时,我也很好地利用了此类技术,但它并不支持我想要使用的所有操作。
在寻找问题的按位解决方案时,我的第一站是网站 bit twiddling hacks 。它有很多适用于许多最常见技术的代码片段。
还有一本书Hacker's Delight 深入介绍了一些按位技术。
While I agree with Michael McGowan in general, bit twiddling hacks can be very useful in certain situations, for instance when programming embedded hardware which doesn't have all the usual instructions. I've also had good use for such techniques when encoding programs into theorem provers such as SMT solvers, which didn't support all the operations I wanted to use.
My first stop when looking for a bitwise solution to a problem is the site bit twiddling hacks. It has quite a few code snippets for many of the most common techniques.
Then there's also the book Hacker's Delight covers a few bitwise techniques in depth.
我在这里发现了一些有趣的按位运算集合:
http://graphics.stanford.edu/~seander/bithacks.html
I found some interesting bitwise operations collection at this place:
http://graphics.stanford.edu/~seander/bithacks.html
按位技巧(用于非位集操作)
|运算符
Covert Upper Case to lower Case letter
输出
解释:
&运算符
Covert Lower Case to Upper Case Letter
输出
解释:**
Determine if a Integer is odd or even
输出:
解释: 如果 n 为奇数,则 (n&1) 返回 1;如果 n 为偶数,则返回 0。
如果 n 是奇数,则二进制表示中 n 的最后一位将始终为 1。这就是为什么当我们 & 时它与 1 的结果变为 1。如果 n 甚至是二进制表示中 n 的最后一位,则始终为 0。这就是为什么当我们 & 时它与1的结果变成0。
(例如(奇)xxxxx1&1=1,(偶)xxxxx0&1=0)
^运算符
Toggle Case
输出
解释:**
Swap two number without temp
输出:
**解释:**
<<运算符
Multiply by 2n
输出: 32
解释: 4x23 =32
Set value 2n
输出: 8
解释: 1*23 =8
>>>运算符
Divide by 2n
输出: 8
解释:
Bitwise tricks (for non-Bitset Operation)
| Operator
Covert Upper Case to lower Case letter
Output
Explanation:
& Operator
Covert Lower Case to Upper Case Letter
Output
Explanation:**
Determine if a Integer is odd or even
Output:
Explanation: (n&1) returns 1 if n is odd and returns 0 when it is even.
If n is odd the last bit of n in binary representation will be always 1. that is why when we & it with 1 the results become 1. If n is even the last bit of n in binary representation will be always 0. that is why when we & it with 1 the results become 0.
(eg. (odd) xxxxx1&1=1, (even) xxxxx0&1=0)
^ operator
Toggle Case
Output
Explanation:**
Swap two number without temp
Output:
**Explanation: **
<< operator
Multiply by 2n
Output: 32
Explanation: 4x23 =32
Set value 2n
Output: 8
Explanation: 1*23 =8
>> operator
Divide by 2n
Output: 8
Explanation:
通常使用这样的技巧并不是一个好主意。现代编译器经常在可能的情况下在幕后做这种事情。也就是说,有时您知道编译器不知道(也许在运行时保证特定值是 2 的幂)。如果您确定尝试这些技巧是个好主意,这里有一些有用的小窍门。
Usually it's not a good idea to use such tricks. Modern compilers often do this kind of stuff behind the scenes when possible. That said, sometimes you have knowledge that the compiler doesn't (maybe that a particular value is guaranteed at runtime to be a power of 2). If you determine that it is a good idea to try such tricks, here are useful bit twiddling hacks.
您始终可以使用左移按位运算符 (<<) 将给定数字乘以 2。
对于前任 -
You can always make use of left shift bitwise operator (<<) to mutiply the given number by 2.
For Ex -