-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
位操作 #7
Comments
检测一个数是不是2的幂
用位运算实现:
运算过程(满足只有一个1):
一些位操作的技巧
|
反码补码反码+1即为补码;
|
比如n=4, 后面的等式效果就是左移27位,即28位为1,其余位为0 |
Ref: Hacker's Delight figure 3-3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
掩码
掩码可以用来清除某些位,保留某些位。
比如:
我们想要清除高4位,保留低4位
得到:
使用AND运算来做。
更普遍的用法是:
从一个很大的字提取单个的字节。我们定义第一个字节为高位。使用 & 和 >> 这2个操作符。
也可以先使用不同的掩码完成。
refs: http://stackoverflow.com/questions/10493411/what-is-bit-masking
The text was updated successfully, but these errors were encountered: