如何创建与面膜至少significat位用设定为1 [英] How to create mask with least significat bits set to 1 in C
问题描述
有人能请解释一下这个功能对我?
Can someone please explain this function to me?
一个面具设置为1的最低显著n位。
A mask with the least significant n bits set to 1.
例如:
N = 6 - >值为0x2F,N = 17 - > 0x1FFFF //我不明白这些可言,尤其是如何每组6 - >值为0x2F
n = 6 --> 0x2F, n = 17 --> 0x1FFFF // I don't get these at all, especially how n = 6 --> 0x2F
此外,什么是面具?
推荐答案
通常的方法是采取 1
和移位它留下ñ
位。这会给你是这样的: 00100000
。然后减去一个来自这一点,这将清除的设置位,并设置所有的少显著位,所以在这种情况下,我们会得到: 00011111
The usual way is to take a 1
, and shift it left n
bits. That will give you something like: 00100000
. Then subtract one from that, which will clear the bit that's set, and set all the less significant bits, so in this case we'd get: 00011111
.
一个面具通常用位操作使用,特别是和
。你会使用上面的面具由自己来获得至少5位显著,二话不说,可能是present隔离。与将通常具有包含比特重新presenting若干完全独立的,不相关的数量和/或标志的单个硬件寄存器硬件处理时,这是特别普遍的。
A mask is normally used with bitwise operations, especially and
. You'd use the mask above to get the 5 least significant bits by themselves, isolated from anything else that might be present. This is especially common when dealing with hardware that will often have a single hardware register containing bits representing a number of entirely separate, unrelated quantities and/or flags.
这篇关于如何创建与面膜至少significat位用设定为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!