英文:
How can I modify bit in position in Go?
问题
我尝试修改特定位置的位,但遇到了问题。
例如,我有一个 1000000001
,我该如何将其修改为 0000000001
?
英文:
I tried to modify bit at a certain position, but ran into a problem.
For example I have 1000000001
, how can I modify it to 0000000001
?
答案1
得分: 3
你可以应用一个位掩码来只保留你感兴趣的位。
在这个例子中,如果你只想要最后一位,你可以应用位掩码 0b0000000001
https://go.dev/play/p/RNQEcON7sw1
// 'x' 是你的值
x := 0b1000000001
// 创建位掩码
mask := 0b0000000001
// 使用按位与运算应用位掩码
output := x&mask
fmt.Println("This value == 1: ", output)
解释
&
是一个按位运算符,表示“与”。它逐位比较两个值,并且只有当两个输入位都为 1
时,结果位才被设置为 1
。下面是与运算符的真值表。
+-----------+----------+--------------+
| 输入位 | 掩码位 | 输入 & 掩码 |
+-----------+----------+--------------+
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
+-----------+----------+--------------+
因为我的掩码函数只在最后一位上有一个 1
,所以只有原始输入的最后一位被保留下来。之前的所有位将始终为 0
。
英文:
You can apply a bitmask to only keep the bits you are interested in.
In this case if you only want the last bit, you apply the bitmask 0b0000000001
https://go.dev/play/p/RNQEcON7sw1
// 'x' is your value
x := 0b1000000001
// Make the bitmask
mask := 0b0000000001
// Apply the bitmask with bitwise AND
output := x&mask
fmt.Println("This value == 1: ", output)
Explaination
&
is a bitwise operator for "AND". Which means it goes through both values bit by bit and sets the resulting bit to 1
if and only if both input bits are 1
. I included a truth table for the AND operator below.
+-----------+----------+--------------+
| Input Bit | Mask Bit | Input & Mask |
+-----------+----------+--------------+
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
+-----------+----------+--------------+
Because my mask function only has a 1
in the last position, only the last position of the original input is kept. All preceding bits will always be 0
.
答案2
得分: 2
构建一个掩码,其中每个位置都设置为要操作的位。
使用按位或操作设置位。
使用按位与操作和反掩码清除位。
使用按位异或操作切换位。
package main
import "fmt"
func main() {
k := 3 // 操作第3位...
mask := uint8(1) << (k - 1) // ... 使用0b00000100作为掩码
var n uint8 = 0b10101010
fmt.Printf("0b%08b\n", n) // 0b10101010
// 设置第k位
n |= mask
fmt.Printf("0b%08b\n", n) // 0b10101110
// 清除第k位
n &^= mask // &^ 是Go的AND NOT运算符
fmt.Printf("0b%08b\n", n) // 0b10101010
// 切换第k位
n ^= mask
fmt.Printf("0b%08b\n", n) // 0b10101110
}
英文:
- Construct a mask that has a one in every place you want to manipulate
- Use bitwise OR to set bits.
- Use bitwise AND with the inverse mask to clear bits.
- Use XOR to toggle a bits
package main
import "fmt"
func main() {
k := 3 // manipulate the 3rd bit ...
mask := uint8(1) << (k - 1) // ... using 0b00000100 as a mask
var n uint8 = 0b10101010
fmt.Printf("0b%08b\n", n) // 0b10101010
// set kth bit
n |= mask
fmt.Printf("0b%08b\n", n) // 0b10101110
// clear kth bit
n &^= mask // &^ is Go's AND NOT operator
fmt.Printf("0b%08b\n", n) // 0b10101010
// toggle kth bit
n ^= mask
fmt.Printf("0b%08b\n", n) // 0b10101110
}
答案3
得分: 0
func test() {
i := 1 << 9 //1000000000
i = i | (1 << 8) //1000000000 | 0100000000 == 1100000000
i = i | (1 << 7) //1100000000 | 0010000000 == 1110000000
i = i | (1 << 0) //1110000000 | 0000000001 == 1110000001
fmt.Printf("BEFORE: %010b\n", i) // 1110000001
i = i & ((1 << 9) - 1) // 1110000001 & ((1000000000) - 1) == 1110000001 & (0111111111) == 0110000001
fmt.Printf("AFTER: %010b\n", i) // 0110000001
}
func test() {
i := 1 << 9 //1000000000
i = i | (1 << 8) //1000000000 | 0100000000 == 1100000000
i = i | (1 << 7) //1100000000 | 0010000000 == 1110000000
i = i | (1 << 0) //1110000000 | 0000000001 == 1110000001
fmt.Printf("BEFORE: %010b\n", i) // 1110000001
i = i & ((1 << 9) - 1) // 1110000001 & ((1000000000) - 1) == 1110000001 & (0111111111) == 0110000001
fmt.Printf("AFTER: %010b\n", i) // 0110000001
}
英文:
func test() {
i := 1 << 9 //1000000000
i = i | (1 << 8) //1000000000 | 0100000000 == 1100000000
i = i | (1 << 7) //1100000000 | 0010000000 == 1110000000
i = i | (1 << 0) //1110000000 | 0000000001 == 1110000001
fmt.Printf("BEFORE: %010b\n", i) // 1110000001
i = i & ((1 << 9) - 1) // 1110000001 & ((1000000000) - 1) == 1110000001 & (0111111111) == 0110000001
fmt.Printf("AFTER: %010b\n", i) // 0110000001
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论