Go 的位操作
<p>在计算机内存昂贵,处理能力有限的美好旧时光里,用比较骇客的位运算方式去处理信息是首选方式(某些情况下只能如此)。时至今日,直接使用位运算仍然是很多计算领域中不可或缺的部分,例如底层系统编程,图形处理,密码学等。</p><p>Go 编程语言支持以下按位运算符:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">& bitwise AND
| bitwise OR
^ bitwise XOR
&^ AND NOT
<< left shift
>> right shift</pre>
</div>
<p>本文的余下部分详述了每个操作符以及它们如何使用的案例。</p>
<h3>& 运算符</h3>
<p>在 Go 中, & 运算符在两个整型操作数中执行按位 AND 操作。AND 操作具有以下属性:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">Given operands a, b
AND(a, b) = 1; only if a = b = 1
else = 0</pre>
</div>
<p>AND 运算符具有选择性的把整型数据的位清除为 0 的好的效果。 例如,我们可以使用 & 运算符去清除(设置)最后 4 个最低有效位(LSB)全部为 0 。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var x uint8 = 0xAC // x = 10101100
x = x & 0xF0 // x = 10100000
}</pre>
</div>
<p>所有的位运算都支持简写的赋值形式。 例如,前面的例子可以重写为如下。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var x uint8 = 0xAC // x = 10101100
x &= 0xF0 // x = 10100000
}</pre>
</div>
<p>另外一个巧妙的技巧是:你可以用 & 操作去测试一个数字是奇数还是偶数。原因是当一个数字的二进制的最低位是 1 的时候,那他就是奇数。我们可以用一个数字和 1 进行 & 操作,然后在和 1 做 AND 运算,如果的到的结果是 1 ,那么这个原始的数字就是奇数</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">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)
}
}
}
</pre>
</div>
<p> </p>
<h3>| 操作符</h3>
<p>| 对其整型操作数执行按位或操作。回想一下或操作符具备以下性质:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">Given operands a, b
OR(a, b) = 1; when a = 1 or b = 1
else = 0 </pre>
</div>
<p>我们可以利用按位或操作符为给定的整数有选择地设置单个位。例如,在如下示例中我们使用按位或将示例数(从低位到高位(MSB))中的第 3 ,第 7 和第 8 位置为 1 。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a uint8 = 0
a |= 196
fmt.Printf("%b", a)
}
// 打印结果11000100</pre>
</div>
<p> </p>
<p>练习场中可运行范例。</p>
<p>在使用位掩码技术为给定的整型数字设置任意位时,或运算非常有用。例如,我们可以扩展之前的程序为变量 a 存储的值设置更多的位。</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a uint8 = 0
a |= 196
a |= 3
fmt.Printf("%b", a)
}
// 打印结果 11000111</pre>
</div>
<p>在前面的程序里,不仅要按位设置十进制的 196,而且要设置低位上的十进制 3。我们还可以继续(或上更多的值)设置完所有的位。</p>
<p>位运算的配置用法<br>现在,回顾一下 AND(a, 1) = a 当且仅当 a = 1。 我们可以利用这个特性去查询其设置位的值。例如,在上述代码中 a & 196 会返回 196 是因为这几位的值在 a 中确实都存在。所以我们可以结合使用 OR 和 AND 运算的方式来分别设置和读取某位的配置值。.</p>
<p>接下来的源码片段演示了这个操作。函数 procstr 会转换字符串的内容。它需要两个参数:第一个, str,是将要被转换的字符串,第二个, conf,是一个使用位掩码的方式指定多重转换配置的整数。</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">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, runes = runes, runes
}
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
}</pre>
</div>
<p>上面的 procstr("HELLO PEOPLE!", LOWER|REV|CAP) 方法会把字符串变成小写,然后反转字符串,最后把字符串里面的单词首字母变成大写。这个功能是通过设置 conf 里的第二,三,四位的值为 14 来完成的。然后代码使用连续的 if 语句块来获取这些位操作进行对应的字符串转换。</p>
<p> </p>
<h3>^ 操作符</h3>
<p>在 Go 中 按位 异或 操作是用 ^ 来表示的。 异或运算符有如下的特点:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">Given operands a, b
XOR(a, b) = 1; only if a != b
else = 0</pre>
</div>
<p>异或运算的这个特性可以用来把二进制位的一个值变成另外一个值。举个例子,给到一个 16 进制的值,我们可以使用以下代码切换前 8 位(从 MSB 开始)的值。</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a uint16 = 0xCEFF
a ^= 0xFF00 // same a = a ^ 0xFF00
}
// a = 0xCEFF (11001110 11111111)
// a ^=0xFF00 (00110001 11111111)</pre>
</div>
<p>在前面的代码片段中,与 1 进行异或的位被翻转(从 0 到 1 或从 1 到 0)。异或 运算的一个实际用途,例如,可以利用 异或运算去比较两个数字的符号是否一样。当 (a ^ b) ≥ 0 (或相反符号的 (a ^ b) < 0 )为 true 的时候,两个整数 a,b 具有相同的符号,如下面的程序所示:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
a, b := -12, 25
fmt.Println("a and b have same sign?", (a ^ b) >= 0)
}</pre>
</div>
<p>当执行上面这个程序的时候,将会打印出:a and b have same sign? false。在 Go Playground 上修改程序里 a ,b 的符号,以便看到不同的结果。</p>
<h3>^ 作为取反位运算符 (非)</h3>
<p>不像其他语言 (c/c++,Java,Python,Javascript,等), Go 没有专门的一元取反位运算符。取而代之的是,XOR 运算符 ^,也可作为一元取反运算符作用于一个数字。对于给定位 x,在 Go 中 x = 1 ^ x 可以翻转该位。在以下的代码段中我们可以看到使用 ^a 获取变量 a 的取反值的操作。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a byte = 0x0F
fmt.Printf("%08b\n", a)
fmt.Printf("%08b\n", ^a)
}
// 打印结果
00001111 // var a
11110000 // ^a
</pre>
</div>
<p> </p>
<h3> </h3>
<h3>&^ 操作符</h3>
<p>&^ 操作符意为 与非,是 与 和 非 操作符的简写形式,它们定义如下。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">Given operands a, b
AND_NOT(a, b) = AND(a, NOT(b))</pre>
</div>
<p>如果第二个操作数为 1 那么它则具有清除第一个操作数中的位的趣味特性。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">AND_NOT(a, 1) = 0; clears a
AND_NOT(a, 0) = a; </pre>
</div>
<p>接下来的代码片段使用 AND NOT 操作符,将变量值 1010 1011 变为 1010 0000,清除了操作数上的低四位。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a byte = 0xAB
fmt.Printf("%08b\n", a)
a &^= 0x0F
fmt.Printf("%08b\n", a)
}
// 打印:
10101011
10100000
</pre>
</div>
<p> </p>
<h3><<和>> 操作符</h3>
<p>与其他 C 的衍生语言类似, Go 使用 << 和 >> 来表示左移运算符和右移运算符,如下所示:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">Given integer operands a and n,
a << n; shifts all bits in a to the left n times
a >> n; shifts all bits in a to the right n times</pre>
</div>
<p>例如,在下面的代码片段中变量 a (00000011)的值将会左移位运算符分别移动三次。每次输出结果都是为了说明左移的目的。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a int8 = 3
fmt.Printf("%08b\n", a)
fmt.Printf("%08b\n", a<<1)
fmt.Printf("%08b\n", a<<2)
fmt.Printf("%08b\n", a<<3)
}
// 输出的结果:
00000011
00000110
00001100
00011000</pre>
</div>
<p>注意每次移动都会将低位右侧补零。相对应,使用右移位操作符进行运算时,每个位均向右方移动,空出的高位补零,如下示例 (有符号数除外,参考下面的算术移位注释)。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a uint8 = 120
fmt.Printf("%08b\n", a)
fmt.Printf("%08b\n", a>>1)
fmt.Printf("%08b\n", a>>2)
}
// 打印:
01111000
00111100
00011110</pre>
</div>
<p>可以利用左移和右移运算中,每次移动都表示一个数的 2 次幂这个特性,来作为某些乘法和除法运算的小技巧。例如,如下代码中,我们可以使用右移运算将 200(存储在变量 a 中)除以 2 。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
a := 200
fmt.Printf("%d\n", a>>1)
}
// 打印:
100</pre>
</div>
<p>或是通过左移 2 位,将一个数乘以 4:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
a := 12
fmt.Printf("%d\n", a<<2)
}
// 打印:
48</pre>
</div>
<p>位移运算符提供了有趣的方式处理二进制值中特定位置的值。例如,下列的代码中,| 和 << 用于设置变量 a 的第三个 bit 位。</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a int8 = 8
fmt.Printf("%08b\n", a)
a = a | (1<<2)
fmt.Printf("%08b\n", a)
}
// prints:
00001000
00001100</pre>
</div>
<p>或者,您可以组合位移运算符和 & 测试是否设置了第 n 位,如下面示例所示:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a int8 = 12
if a&(1<<2) != 0 {
fmt.Println("take action")
}
}
// 打印:
take action</pre>
</div>
<p>使用 &^ 和位移运算符,我们可以取消设置一个值的某个位。例如,下面的示例将变量 a 的第三位置为 0 :</p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">func main() {
var a int8 = 13
fmt.Printf("%04b\n", a)
a = a &^ (1 << 2)
fmt.Printf("%04b\n", a)
}
// 打印:
1101
1001</pre>
</div>
<p>关于算术位移运算的笔记<br>当要位移的值(左操作数)是有符号值时,Go 自动应用算术位移。在右移操作期间,复制(或扩展)二进制补码符号位以填充位移的空隙。</p>
<p>关于c语言实现一些位操作记录:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">交换两个数
#include </span><stdio.h>
<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> main () {
</span><span style="color: rgba(0, 0, 255, 1)">int</span> a = <span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> b = <span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">;
printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a:%d b:%d\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,a,b);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">c语言中,int类型占四个字节
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a:12在内存中表示如下:00000000 00000000 00000000 00001100
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">b:3在内存中表示如下: 00000000 00000000 00000000 00000011
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">^为按位异或运算符:对应位不同则为1,相同则为0</span>
a = a^b;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">00000000 00000000 00000000 00001111 :a的结果为15 </span>
b = a^b;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">00000000 00000000 00000000 00001100 :b的结果为12</span>
a = a^b;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">00000000 00000000 00000000 00000011 :最后a的结果为3</span>
printf(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a:%d b:%d\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,a,b);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}
返回整数二进制中1的个数
#include </span><stdio.h>
<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> main () {
</span><span style="color: rgba(0, 0, 255, 1)">int</span> a = <span style="color: rgba(128, 0, 128, 1)">122</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">int</span> sum = <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)"> ;
</span><span style="color: rgba(0, 0, 255, 1)">while</span>(a!=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">&在c语言中为按位与,对应位都为1,则该位为1,反之相反,常用来清零,判断。
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1在内存中的表示:00000000 00000000 00000000 00000001
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">a&1:判断a的最低位是否为1</span>
sum+=a&<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">>>在c语言中为右移位,如:00000000 00000000 00000000 00001110,右移一位为:00000000 00000000 00000000 00000111
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">其实就是每往右移一位则除以2</span>
a = a>><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}
printf(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">sum:%d\n</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,sum);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p> </p>
<p> </p>
<p>原文地址:https://dev.to/vladimirvivien/bit-hackin...</p>
<p>译文地址:https://learnku.com/go/t/23460/bit-opera...</p>
<p>原文作者:Ellison<br>转自链接:https://learnku.com/go/t/23460/bit-operation-of-go<br><br></p><br><br>
来源:https://www.cnblogs.com/-wenli/p/12419686.html
頁:
[1]