How can I modify bit in position in Go?

huangapple go评论78阅读模式
英文:

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
}
英文:
  1. Construct a mask that has a one in every place you want to manipulate
  2. Use bitwise OR to set bits.
  3. Use bitwise AND with the inverse mask to clear bits.
  4. Use XOR to toggle a bits
package main

import &quot;fmt&quot;

func main() {
	k := 3                      // manipulate the 3rd bit ...
	mask := uint8(1) &lt;&lt; (k - 1) // ... using 0b00000100 as a mask

	var n uint8 = 0b10101010
	fmt.Printf(&quot;0b%08b\n&quot;, n) // 0b10101010

	// set kth bit
	n |= mask
	fmt.Printf(&quot;0b%08b\n&quot;, n) // 0b10101110

	// clear kth bit
	n &amp;^= mask                // &amp;^ is Go&#39;s AND NOT operator
	fmt.Printf(&quot;0b%08b\n&quot;, n) // 0b10101010

	// toggle kth bit
	n ^= mask
	fmt.Printf(&quot;0b%08b\n&quot;, 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 &lt;&lt; 9 //1000000000

	i = i | (1 &lt;&lt; 8) //1000000000 | 0100000000 == 1100000000
	i = i | (1 &lt;&lt; 7) //1100000000 | 0010000000 == 1110000000
	i = i | (1 &lt;&lt; 0) //1110000000 | 0000000001 == 1110000001

	fmt.Printf(&quot;BEFORE: %010b\n&quot;, i) // 1110000001

	i = i &amp; ((1 &lt;&lt; 9) - 1) // 1110000001 &amp; ((1000000000) - 1) == 1110000001 &amp; (0111111111) == 0110000001

	fmt.Printf(&quot;AFTER: %010b\n&quot;, i) // 0110000001
}

huangapple
  • 本文由 发表于 2022年2月17日 21:44:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71159294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定