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 > right shift
The remainder of this article details each operator and their How to use it.
& Operator
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 <p>Run the above example on <a href="https://play.golang.org/p/2mTNOtioNM" target="_blank">Playground</a></p><h4> <span class="header-link octicon octicon-link"></span><strong>| Operator</strong> </h4><p>##|<code> Performs bitwise </code> or <code> operations on its integer operands. Recall that the </code> or <code> operator has the following properties: </code></p><pre class="brush:php;toolbar:false">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.
Configuration usage of bit operations
Now, reviewAND(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 Run the code on the Playground.<p></p>The above <p>procstr("HELLO PEOPLE!", LOWER|REV|CAP)<code> 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 </code>conf<code> to 14. The code then uses successive if statement blocks to obtain these bit operations and perform corresponding string conversions. </code></p><h4> <span class="header-link octicon octicon-link"></span>^ Operator<strong></strong> </h4>In Go, the bitwise <p>XOR<code> operation is done using </code>^<code> to express. </code>The XOR<code> operator has the following characteristics: </code></p><pre class="brush:php;toolbar:false">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) )为 <code>true
的时候,两个整数 a,b 具有相同的符号,如下面的程序所示:
func main() { a, b := -12, 25 fmt.Println(&amp;amp;quot;a and b have same sign?&amp;amp;quot;, (a ^ b) &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;quot;%08b\n&amp;amp;quot;, a) fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, ^a) } // 打印结果 00001111 // var a 11110000 // ^a
在练习场中可以运行范例。
&amp;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;quot;%08b\n&amp;amp;quot;, a) a &amp;amp;amp;^= 0x0F fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a) } // 打印: 10101011 10100000
在练习场中运行范例。
> 操作符
与其他 C 的衍生语言类似, Go 使用 和 <code>>>
来表示左移运算符和右移运算符,如下所示:
Given integer operands a and n, a &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;quot;%08b\n&amp;amp;quot;, a) fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a&amp;amp;lt;p&amp;amp;gt;在 &amp;amp;lt;a href=&amp;amp;quot;https://play.golang.org/p/_lhE8OoZxY&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;Playground&amp;amp;lt;/a&amp;amp;gt; 运行代码&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;注意每次移动都会将低位右侧补零。相对应,使用右移位操作符进行运算时,每个位均向右方移动,空出的高位补零,如下示例 (有符号数除外,参考下面的算术移位注释)。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;pre class=&amp;amp;quot;brush:php;toolbar:false&amp;amp;quot;&amp;amp;gt;func main() { var a uint8 = 120 fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a) fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a&amp;amp;gt;&amp;amp;gt;1) fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a&amp;amp;gt;&amp;amp;gt;2) } // 打印: 01111000 00111100 00011110
在 练习场中可以运行范例。
可以利用左移和右移运算中,每次移动都表示一个数的 2 次幂这个特性,来作为某些乘法和除法运算的小技巧。例如,如下代码中,我们可以使用右移运算将 200
(存储在变量 a 中)除以 2 。
func main() { a := 200 fmt.Printf(&amp;amp;quot;%d\n&amp;amp;quot;, a&amp;amp;gt;&amp;amp;gt;1) } // 打印: 100
在 练习场 中可以运行范例。
或是通过左移 2 位,将一个数乘以4:
func main() { a := 12 fmt.Printf(&amp;amp;quot;%d\n&amp;amp;quot;, a&amp;amp;lt;p&amp;amp;gt;在 &amp;amp;lt;a href=&amp;amp;quot;https://play.golang.org/p/xuJRcKgMVV&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;练习场&amp;amp;lt;/a&amp;amp;gt; 中可以运行范例。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;位移运算符提供了有趣的方式处理二进制值中特定位置的值。例如,下列的代码中,&amp;amp;lt;code&amp;amp;gt;|&amp;amp;lt;/code&amp;amp;gt; 和 &amp;amp;lt;code&amp;amp;gt; 用于设置变量 &amp;amp;lt;code&amp;amp;gt;a&amp;amp;lt;/code&amp;amp;gt; 的第三个 bit 位。&amp;amp;lt;/code&amp;amp;gt;&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;pre class=&amp;amp;quot;brush:php;toolbar:false&amp;amp;quot;&amp;amp;gt;func main() { var a int8 = 8 fmt.Printf(&amp;amp;quot;%08b\n&amp;amp;quot;, a) a = a | (1&amp;amp;lt;p&amp;amp;gt;可以在 &amp;amp;lt;a href=&amp;amp;quot;https://play.golang.org/p/h7WoP7ieuI&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;练习场&amp;amp;lt;/a&amp;amp;gt; 中运行代码示例。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;或者,您可以组合位移运算符和 &amp;amp;lt;code&amp;amp;gt;&amp;amp;amp;&amp;amp;lt;/code&amp;amp;gt; 测试是否设置了第n位,如下面示例所示:&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;pre class=&amp;amp;quot;brush:php;toolbar:false&amp;amp;quot;&amp;amp;gt;func main() { var a int8 = 12 if a&amp;amp;amp;(1&amp;amp;lt;p&amp;amp;gt;在 &amp;amp;lt;a href=&amp;amp;quot;https://play.golang.org/p/Ptc7Txk5Jb&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;练习场&amp;amp;lt;/a&amp;amp;gt;中运行代码。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;使用 &amp;amp;lt;code&amp;amp;gt;&amp;amp;amp;^&amp;amp;lt;/code&amp;amp;gt; 和位移运算符,我们可以取消设置一个值的某个位。例如,下面的示例将变量 a 的第三位置为 0 :&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;pre class=&amp;amp;quot;brush:php;toolbar:false&amp;amp;quot;&amp;amp;gt;func main() { var a int8 = 13 fmt.Printf(&amp;amp;quot;%04b\n&amp;amp;quot;, a) a = a &amp;amp;amp;^ (1 &amp;amp;lt;p&amp;amp;gt;在 &amp;amp;lt;a href=&amp;amp;quot;https://play.golang.org/p/Stjq9oOjKz&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;练习场&amp;amp;lt;/a&amp;amp;gt; 中运行代码。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;h4&amp;amp;gt; &amp;amp;lt;span class=&amp;amp;quot;header-link octicon octicon-link&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;关于算术位移运算的笔记&amp;amp;lt;/strong&amp;amp;gt; &amp;amp;lt;/h4&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;当要位移的值(左操作数)是有符号值时,Go 自动应用算术位移。在右移操作期间,复制(或扩展)二进制补码符号位以填充位移的空隙。&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;h4&amp;amp;gt; &amp;amp;lt;span class=&amp;amp;quot;header-link octicon octicon-link&amp;amp;quot;&amp;amp;gt;&amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;strong&amp;amp;gt;Summary&amp;amp;lt;/strong&amp;amp;gt; &amp;amp;lt;/h4&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;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 &amp;amp;lt;a href=&amp;amp;quot;https://graphics.stanford.edu/~seander/bithacks.html&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;Bit Twiddling Hacks&amp;amp;lt;/a&amp;amp;gt; written by Sean Eron Anderson. &amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;Follow Vladim &amp;amp;lt;a href=&amp;amp;quot;https://twitter.com/VladimirVivien&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;@vladimirvivien&amp;amp;lt;/a&amp;amp;gt; on Twitter. &amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;If you are learning Go, read Vladimir Vivien's book about Go called &amp;amp;lt;a href=&amp;amp;quot;https://www.packtpub.com/application-development/learning-go-programming&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;Learning Go Programming&amp;amp;lt;/a&amp;amp;gt;. &amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;This article was originally published on Medium by author Vladimir Vivien as &amp;amp;lt;a href=&amp;amp;quot;https://medium.com/learning-the-go-programming-language/bit-hacking-with-go-e0acee258827#.2hof13ia5&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot;&amp;amp;gt;Bit Hacking with Go&amp;amp;lt;/a&amp;amp;gt;. &amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;blockquote&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;English original address: https://dev.to/vladimirvivien/bit-hacking-with-go&amp;amp;lt;/p&amp;amp;gt;&amp;amp;lt;/blockquote&amp;amp;gt;&amp;amp;lt;p&amp;amp;gt;For more programming-related knowledge, please visit: &amp;amp;lt;a href=&amp;amp;quot;https://www.php.cn/course.html&amp;amp;quot; target=&amp;amp;quot;_blank&amp;amp;quot; textvalue=&amp;amp;quot;编程视频&amp;amp;quot;&amp;amp;gt;programmingvideo&amp;amp;lt;/a&amp;amp;gt;! ! &amp;amp;lt;/p&amp;amp;gt;
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!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

闭包(closure)是一个函数以及其捆绑的周边环境状态(lexical environment,词法环境)的引用的组合。 换而言之,闭包让开发者可以从内部函数访问外部函数的作用域。 闭包会随着函数的创建而被同时创建。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

在写 Go 的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,下面本篇就来聊聊 Go 自带的 HttpClient 的超时机制,希望对大家有所帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
