在 Golang 中,您如何设置和清除整数的个别位?例如,行为类似于以下函数的函数:
clearBit(129, 7) // returns 1
setBit(1, 7) // returns 129
clearBit(129, 7) // returns 1
setBit(1, 7) // returns 129
这里有一个设置比特位的函数。首先,在整型数中将数字1向左移指定数量的空间(这样它就变成了0010、0100等)。然后,将其与原始输入进行按位“或”运算。这会使其他位不受影响,但总是将目标比特位设置为1。
// Sets the bit at pos in the integer n.
func setBit(n int, pos uint) int {
n |= (1 << pos)
return n
}
这是一个清除位的函数。首先将数字1向整数中指定的位移动(因此它变为0010、0100等)。然后使用^运算符翻转掩码中的每个位(因此0010变为1101)。然后使用按位AND运算,该运算不会触及与1进行'AND'的数字,并且将取消设置为0的掩码中的值。// Clears the bit at pos in n.
func clearBit(n int, pos uint) int {
mask := ^(1 << pos)
n &= mask
return n
}
最后,这是一个检查特定位是否设置的函数。将数字1向左移动指定的位数(因此它变为0010、0100等),然后与目标数字进行AND运算。如果结果大于0(它将是1、2、4、8等),则该位被设置。
func hasBit(n int, pos uint) bool {
val := n & (1 << pos)
return (val > 0)
}
还有一种清除位的简洁符号表示法。该运算符为&^
,称为"与非"。
使用此运算符,clearBit
函数可以编写如下:
// Clears the bit at pos in n.
func clearBit(n int, pos uint) int {
n &^= (1 << pos)
return n
}
或者像这样:
// Clears the bit at pos in n.
func clearBit(n int, pos uint) int {
return n &^ (1 << pos)
}
x &= ^(1<<7)
或x |= 1<<7
(或x ^= 1<<7
进行切换,或(x & 1<<7 == 0)
查看位是否设置)。这已经被广泛理解,不需要注释,但如果您觉得应该解释一下(例如,您的团队不来自位操作的世界),请在每个惯用语第一次使用时进行注释。此外,人们经常定义表示位或位组合的常量,以便不必每次都写出1<<n
并记住正确的n
,而iota
可以帮助使这些常量声明简洁。 - twotwotwo