Home > Article > Backend Development > A detailed explanation of bit operations in Golang
This article will give you an in-depth understanding of bit operations in Golang, and introduce cases detailing each operator and how to use them. I hope it will be helpful to everyone!
In the good old days when computer memory was expensive and processing power was limited, using hacker-style bit operations to process information was the preferred method (in some cases only in this way). To this day, the direct use of bit operations is still an integral part of many computing fields, such as low-level system programming, graphics processing, cryptography, etc. [Related recommendations: Go Video Tutorial]
The Go programming language supports the following bitwise operators:
& bitwise AND | bitwise OR ^ bitwise XOR &^ AND NOT << left shift >> right shift
The remainder of this article details each operator and their How to use it.
In Go, the &
operator performs the following operations on two integer operands: Bit AND
operations. The AND
operation has the following properties:
Given operands a, b AND(a, b) = 1; only if a = b = 1 else = 0
AND
The operator has the good effect of selectively clearing the bits of integer data to 0. For example, we can use the &
operator to clear (set) the last 4 least significant bits (LSB) to all 0.
func main() { var x uint8 = 0xAC // x = 10101100 x = x & 0xF0 // x = 10100000 }
All bit operations support abbreviated assignment forms. For example, the previous example could be rewritten as follows.
func main() { var x uint8 = 0xAC // x = 10101100 x &= 0xF0 // x = 10100000 }
Another neat trick is: you can use the &
operation to test whether a number is odd or even. The reason is that when the lowest bit of a number is 1, it is an odd number. We can use a number and 1 to perform the &
operation, and then perform the AND
operation with 1. If the result is 1, then the original number is an odd number
import ( "fmt" "math/rand" ) func main() { for x := 0; x < 100; x++ { num := rand.Int() if num&1 == 1 { fmt.Printf("%d is odd\n", num) } else { fmt.Printf("%d is even\n", num) } } }
Run the above example on Playground
##| Performs bitwise
or operations on its integer operands. Recall that the
or operator has the following properties:
Given operands a, b OR(a, b) = 1; when a = 1 or b = 1 else = 0We can use the bitwise
or operator to selectively set individual bits for a given integer. For example, in the following example we use bitwise
or to set the 3rd, 7th and 8th bits in the example number (from low bit to high bit (MSB)) to 1.
func main() { var a uint8 = 0 a |= 196 fmt.Printf("%b", a) } // 打印结果 11000100 ^^ ^
Examples can be run in the driving range.
The or operation is useful when using bit masking techniques to set arbitrary bits for a given integer number. For example, we could extend the previous program to set more bits for the value stored in variable
a.
func main() { var a uint8 = 0 a |= 196 a |= 3 fmt.Printf("%b", a) } // 打印结果 11000111Examples can be run in the
Practice Field.
In the previous program, not only the decimal 196 must be set bitwise, but also the decimal 3 in the low bit must be set. We can continue (more values on or ) to set all the bits.
AND(a, 1) = a if and only if a = 1. We can use this feature to query the value of its setting bit. For example, in the above code
a & 196 will return 196 because the values of these bits do exist in
a. So we can use the
OR and
AND operations in combination to set and read the configuration value of a certain bit respectively. .
procstr converts the contents of a string. It requires two parameters: the first,
str, is the string to be converted, and the second,
conf, is a bit mask that specifies multiple conversion configurations. integer.
const ( UPPER = 1 // 大写字符串 LOWER = 2 // 小写字符串 CAP = 4 // 字符串单词首字母大写 REV = 8 // 反转字符串 ) func main() { fmt.Println(procstr("HELLO PEOPLE!", LOWER|REV|CAP)) } func procstr(str string, conf byte) string { // 反转字符串 rev := func(s string) string { runes := []rune(s) n := len(runes) for i := 0; i < n/2; i++ { runes[i], runes[n-1-i] = runes[n-1-i], runes[i] } return string(runes) } // 查询配置中的位操作 if (conf & UPPER) != 0 { str = strings.ToUpper(str) } if (conf & LOWER) != 0 { str = strings.ToLower(str) } if (conf & CAP) != 0 { str = strings.Title(str) } if (conf & REV) != 0 { str = rev(str) } return str }Run the code on the Playground.The above
procstr("HELLO PEOPLE!", LOWER|REV|CAP) method will turn the string into lower case. Then reverse the string, and finally capitalize the first letters of the words in the string. This function is accomplished by setting the second, third, and fourth digits in
conf to 14. The code then uses successive if statement blocks to obtain these bit operations and perform corresponding string conversions.
XOR operation is done using
^ to express.
The XOR operator has the following characteristics:
Given operands a, b XOR(a, b) = 1; only if a != b else = 0
This characteristic of the XOR operation can be used to change one value of a binary bit into another value. For example, given a hexadecimal value, we can use the following code to switch the first 8 bits (starting from MSB) of the value.
func main() { var a uint16 = 0xCEFF a ^= 0xFF00 // same a = a ^ 0xFF00 } // a = 0xCEFF (11001110 11111111) // a ^=0xFF00 (00110001 11111111)
在前面的代码片段中,与 1 进行异或的位被翻转(从 0 到 1 或从 1 到 0)。异或
运算的一个实际用途,例如,可以利用 异或
运算去比较两个数字的符号是否一样。当 (a ^ b) ≥ 0
(或相反符号的 (a ^ b) < 0
)为 true
的时候,两个整数 a,b 具有相同的符号,如下面的程序所示:
func main() { a, b := -12, 25 fmt.Println(&amp;amp;amp;quot;a and b have same sign?&amp;amp;amp;quot;, (a ^ b) &amp;amp;amp;gt;= 0) }
在 Go 的 Playground运行代码。
当执行上面这个程序的时候,将会打印出:a and b have same sign? false
。在 Go Playground 上修改程序里 a ,b 的符号,以便看到不同的结果。
不像其他语言 (c/c++,Java,Python,Javascript,等), Go 没有专门的一元取反位运算符。取而代之的是,XOR
运算符 ^
,也可作为一元取反运算符作用于一个数字。对于给定位 x,在 Go 中 x = 1 ^ x 可以翻转该位。在以下的代码段中我们可以看到使用 ^a
获取变量 a
的取反值的操作。
func main() { var a byte = 0x0F fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, ^a) } // 打印结果 00001111 // var a 11110000 // ^a
在练习场中可以运行范例。
&amp;amp;amp;^
操作符意为 与非
,是 与
和 非
操作符的简写形式,它们定义如下。
Given operands a, b AND_NOT(a, b) = AND(a, NOT(b))
如果第二个操作数为 1 那么它则具有清除第一个操作数中的位的趣味特性。
AND_NOT(a, 1) = 0; clears a AND_NOT(a, 0) = a;
接下来的代码片段使用 AND NOT
操作符,将变量值1010 1011
变为 1010 0000
,清除了操作数上的低四位。
func main() { var a byte = 0xAB fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) a &amp;amp;amp;amp;^= 0x0F fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) } // 打印: 10101011 10100000
在练习场中运行范例。
与其他 C 的衍生语言类似, Go 使用 <<
和 来表示左移运算符和右移运算符,如下所示:
Given integer operands a and n, a &amp;amp;amp;lt;&amp;amp;amp;lt; n; shifts all bits in a to the left n times a &amp;amp;amp;gt;&amp;amp;amp;gt; n; shifts all bits in a to the right n times
例如,在下面的代码片段中变量 a
(00000011
)的值将会左移位运算符分别移动三次。每次输出结果都是为了说明左移的目的。
func main() { var a int8 = 3 fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a&amp;amp;amp;lt;&amp;amp;amp;lt;1) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a&amp;amp;amp;lt;&amp;amp;amp;lt;2) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a&amp;amp;amp;lt;&amp;amp;amp;lt;3) } // 输出的结果: 00000011 00000110 00001100 00011000
在 Playground 运行代码
注意每次移动都会将低位右侧补零。相对应,使用右移位操作符进行运算时,每个位均向右方移动,空出的高位补零,如下示例 (有符号数除外,参考下面的算术移位注释)。
func main() { var a uint8 = 120 fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a&amp;amp;amp;gt;&amp;amp;amp;gt;1) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a&amp;amp;amp;gt;&amp;amp;amp;gt;2) } // 打印: 01111000 00111100 00011110
在 练习场中可以运行范例。
可以利用左移和右移运算中,每次移动都表示一个数的 2 次幂这个特性,来作为某些乘法和除法运算的小技巧。例如,如下代码中,我们可以使用右移运算将 200
(存储在变量 a 中)除以 2 。
func main() { a := 200 fmt.Printf(&amp;amp;amp;quot;%d\n&amp;amp;amp;quot;, a&amp;amp;amp;gt;&amp;amp;amp;gt;1) } // 打印: 100
在 练习场 中可以运行范例。
或是通过左移 2 位,将一个数乘以4:
func main() { a := 12 fmt.Printf(&amp;amp;amp;quot;%d\n&amp;amp;amp;quot;, a&amp;amp;amp;lt;&amp;amp;amp;lt;2) } // 打印: 48
在 练习场 中可以运行范例。
位移运算符提供了有趣的方式处理二进制值中特定位置的值。例如,下列的代码中,|
和 <<
用于设置变量 a
的第三个 bit 位。
func main() { var a int8 = 8 fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) a = a | (1&amp;amp;amp;lt;&amp;amp;amp;lt;2) fmt.Printf(&amp;amp;amp;quot;%08b\n&amp;amp;amp;quot;, a) } // prints: 00001000 00001100
可以在 练习场 中运行代码示例。
或者,您可以组合位移运算符和 &amp;amp;amp;
测试是否设置了第n位,如下面示例所示:
func main() { var a int8 = 12 if a&amp;amp;amp;amp;(1&amp;amp;amp;lt;&amp;amp;amp;lt;2) != 0 { fmt.Println(&amp;amp;amp;quot;take action&amp;amp;amp;quot;) } } // 打印: take action
在 练习场中运行代码。
使用 &amp;amp;amp;^
和位移运算符,我们可以取消设置一个值的某个位。例如,下面的示例将变量 a 的第三位置为 0 :
func main() { var a int8 = 13 fmt.Printf(&amp;amp;amp;quot;%04b\n&amp;amp;amp;quot;, a) a = a &amp;amp;amp;amp;^ (1 &amp;amp;amp;lt;&amp;amp;amp;lt; 2) fmt.Printf(&amp;amp;amp;quot;%04b\n&amp;amp;amp;quot;, a) } // 打印: 1101 1001
在 练习场 中运行代码。
当要位移的值(左操作数)是有符号值时,Go 自动应用算术位移。在右移操作期间,复制(或扩展)二进制补码符号位以填充位移的空隙。
Like other modern operators, Go supports all binary bit manipulation operators. This article merely provides examples of the various hacks that can be accomplished with these operators. You can find many articles on the web, especially Bit Twiddling Hacks written by Sean Eron Anderson.
Follow Vladim @vladimirvivien on Twitter.
If you are learning Go, read Vladimir Vivien's book about Go called Learning Go Programming.
This article was originally published on Medium by author Vladimir Vivien as Bit Hacking with Go.
English original address: https://dev.to/vladimirvivien/bit-hacking-with-go
For more programming-related knowledge, please visit: programmingvideo! !
The above is the detailed content of A detailed explanation of bit operations in Golang. For more information, please follow other related articles on the PHP Chinese website!