A brief analysis of bit operations (bitwise operators) in Golang

青灯夜游
Release: 2023-02-24 19:42:25
forward
4439 people have browsed it

A brief analysis of bit operations (bitwise operators) in Golang

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 way (and in some cases the only way to do it). 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.

The Go programming language supports the following bitwise operators:

&   bitwise AND
 |   bitwise OR
 ^   bitwise XOR
&^   AND NOT
<<   left shift
>>   right shift
Copy after login

The remainder of this article details each operator and examples of how they are used.

& Operator

In Go, the & operator performs a bitwise AND# between two integer operands ## operate. The AND operation has the following properties:

Given operands a, b
AND(a, b) = 1; only if a = b = 1
               else = 0
Copy after login

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
}
Copy after login

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
}
Copy after login

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)
        }
    }
}
Copy after login

Run the above example on

Playground

| Operator

| operates on its integer operand Perform a bitwise or operation. Recall that the or operator has the following properties:

Given operands a, b
OR(a, b) = 1; when a = 1 or b = 1
              else = 0
Copy after login

We 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
            ^^   ^
Copy after login

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)
}

// 打印结果 11000111
Copy after login

Examples 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, review

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. .

The following source code snippet demonstrates this operation. Function

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
}
Copy after login

Run the code on

Playground.

The above

procstr("HELLO PEOPLE!", LOWER|REV|CAP) method will Change the string to lowercase, then reverse the string, and finally make the first letters of the words in the string uppercase. 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.

^ Operator

In Go, the bitwise

XOR operation is represented by ^. The XOR operator has the following characteristics:

Given operands a, b
XOR(a, b) = 1; only if a != b
     else = 0
Copy after login

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)
Copy after login

在前面的代码片段中,与 1 进行异或的位被翻转(从 0 到 1 或从 1 到 0)。异或 运算的一个实际用途,例如,可以利用 异或运算去比较两个数字的符号是否一样。当 (a ^ b) ≥ 0 (或相反符号的 (a ^ b) < 0 )为 true 的时候,两个整数 a,b 具有相同的符号,如下面的程序所示:

func main() {
    a, b := -12, 25
    fmt.Println("a and b have same sign?", (a ^ b) >= 0)
}
Copy after login

在 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("%08b\n", a)
    fmt.Printf("%08b\n", ^a)
}

// 打印结果
00001111     // var a
11110000     // ^a
Copy after login

练习场中可以运行范例。

&^ 操作符

&^ 操作符意为 与非,是 操作符的简写形式,它们定义如下。

Given operands a, b
AND_NOT(a, b) = AND(a, NOT(b))
Copy after login

如果第二个操作数为 1 那么它则具有清除第一个操作数中的位的趣味特性。

AND_NOT(a, 1) = 0; clears a
AND_NOT(a, 0) = a;
Copy after login

接下来的代码片段使用 AND NOT 操作符,将变量值1010 1011变为 1010 0000,清除了操作数上的低四位。

func main() {
    var a byte = 0xAB
     fmt.Printf("%08b\n", a)
     a &^= 0x0F
     fmt.Printf("%08b\n", a)
}

// 打印:
10101011
10100000
Copy after login

练习场中运行范例。

<< 和 >> 操作符

与其他 C 的衍生语言类似, Go 使用 << 来表示左移运算符和右移运算符,如下所示:

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
Copy after login

例如,在下面的代码片段中变量 a00000011)的值将会左移位运算符分别移动三次。每次输出结果都是为了说明左移的目的。

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
Copy after login

Playground 运行代码

注意每次移动都会将低位右侧补零。相对应,使用右移位操作符进行运算时,每个位均向右方移动,空出的高位补零,如下示例 (有符号数除外,参考下面的算术移位注释)。

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
Copy after login

练习场中可以运行范例。

可以利用左移和右移运算中,每次移动都表示一个数的 2 次幂这个特性,来作为某些乘法和除法运算的小技巧。例如,如下代码中,我们可以使用右移运算将 200(存储在变量 a 中)除以 2 。

func main() {
    a := 200
    fmt.Printf("%d\n", a>>1)
}

// 打印:
100
Copy after login

练习场 中可以运行范例。

或是通过左移 2 位,将一个数乘以4:

func main() {
    a := 12
    fmt.Printf("%d\n", a<<2)
}
// 打印:

48
Copy after login

练习场 中可以运行范例。

位移运算符提供了有趣的方式处理二进制值中特定位置的值。例如,下列的代码中,|<< 用于设置变量 a 的第三个 bit 位。

func main() {
    var a int8 = 8
    fmt.Printf("%08b\n", a)
    a = a | (1<<2)
    fmt.Printf("%08b\n", a)
}
// prints:
00001000
00001100
Copy after login

可以在 练习场 中运行代码示例。

或者,您可以组合位移运算符和 & 测试是否设置了第n位,如下面示例所示:

func main() {
    var a int8 = 12
    if a&(1<<2) != 0 {
        fmt.Println("take action")
    }
}

// 打印:
take action
Copy after login

练习场中运行代码。

使用 &^ 和位移运算符,我们可以取消设置一个值的某个位。例如,下面的示例将变量 a 的第三位置为 0 :

func main() {
    var a int8 = 13 
    fmt.Printf("%04b\n", a)
    a = a &^ (1 << 2)
    fmt.Printf("%04b\n", a)
}

// 打印:
1101
1001
Copy after login

练习场 中运行代码。

关于算术位移运算的笔记

当要位移的值(左操作数)是有符号值时,Go 自动应用算术位移。在右移操作期间,复制(或扩展)二进制补码符号位以填充位移的空隙。

Summary

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.

Original address: https://dev.to/vladimirvivien/bit-hacking-with-go

Translation address: https://learnku.com/go/t/ 23460/bit-operation-of-go

Recommended learning: Golang tutorial

The above is the detailed content of A brief analysis of bit operations (bitwise operators) in Golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template