Home >Backend Development >Golang >A brief analysis of bit operations (bitwise operators) in Golang

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

青灯夜游
青灯夜游forward
2023-02-24 19:42:254486browse

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

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

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

| 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

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

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

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
}

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

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) 087d86671d155612d4f02e9d24199fd2> 操作符<p>与其他 C 的衍生语言类似, Go 使用 <code>036118feef1720dceedad2101673ba7c> 来表示左移运算符和右移运算符,如下所示:

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

例如,在下面的代码片段中变量 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

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

练习场中可以运行范例。

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

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

// 打印:
100

练习场 中可以运行范例。

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

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

48

练习场 中可以运行范例。

位移运算符提供了有趣的方式处理二进制值中特定位置的值。例如,下列的代码中,|<< 用于设置变量 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

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

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

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

// 打印:
take action

练习场中运行代码。

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

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

// 打印:
1101
1001

练习场 中运行代码。

关于算术位移运算的笔记

当要位移的值(左操作数)是有符号值时,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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete