1024programmer Java Differences between some operators “|”, “^”, “&”, “&^”.Golang

Differences between some operators “|”, “^”, “&”, “&^”.Golang

Recently I read the golang specification and faced some interesting operators:

& bitwise AND integers
 | bitwise OR integers
 ^ bitwise XOR integers
 &^ bit clear (AND NOT) integers
 

I tried playing around with it, but the only thing I understand is that “|” adds integers and the “+” operator additionally works with floats, strings, etc.

What are they used for in practice? Can someone provide some explanations for these 4 operators above?

1> icza..:


When you must use byte or bit-level data, bitwise The operator comes into play.

Here I list some examples of bit manipulation using code examples (in no particular order):


1.They are common in many algorithms as part of encryption and hash functions (such as MD5).

2.If you want to “save” space and you pack multiple “bool” variables into one variable, they are also often Being used with int, for example, you assign a bit to each bool variable. You have to use bitwise operators to change/read the bits individually.

For example, packing 8 bits/bool into an int:

flags := 0x00 // All flags are 0
 flags |= 0x02 // Turn the 2nd bit to 1 (leaving rest unchanged)
 flags |= 0xff // Turn 8 bits (0..7) to 1
 flags &= 0xfe // Set the lowest bit to 0 (leaving rest unchanged)

 istrue := flags&0x04 != 0 // Test if 3rd bit is 1
 

3.Another area is compressed data where you want to get the most out of data, byte and use All its bits to store/retrieve some information (somewhat the basic unit of information in computing and digital communications).

4. Similar to but not identical to compression: Bitstream. It is also used to compress by not sending complete bytes but instead sending fields with arbitrary bit lengths. Save space in the data stream.

I have written and released a highly optimized bit-level reader and writer package, open source here: github.com/icza/bitio. You will see various bits in its source Extensive use of operations.

5.Another practical use: Testing certain properties of (integer) numbers. Knowing the binary representation of integers (two’s complement), and the existence of numbers in binary representations Certain characteristics of . For example, if the lowest bit is 0, then the integer (in 2’s complement) is even (can be divided by 2):

func isEven(i int) bool {
     return i&0x01 == 0
 }
 

You can also tell whether an integer is a power of 2 by testing its bits. For example, if a positive number contains only one 1 bit, it is a power of 2 (e.g. 2 = 0x02 = 00000010b, 16 = 0x10 = 00010000 but 17 = 0x11 = 00010001 is not a power of 2).

6. Many encoding/decoding processes also use bit operations. The simplest is UTF-8 encoding, which uses a variable-length encoding to convert unicode code points (rune in Go) is represented as a sequence of bytes.
A simple variation of variable length encoding could be to use the highest bit of the byte (8th or 7th if 0 indexed) to indicate if more bytes are needed to decode the number, while the remaining 7 bits are always ” Useful ” ” data. You can test the highest bit and “separate” the 7 useful bits like this:

b := readOneByte()
 usefulBits := b & 0x7f
 hasMoreBytes := b & 0x80 != 0
 

The advantage of using this variable-length encoding is that even if you uint64 use an 8-byte Go type in memory, you can still represent small numbers (in the range) with fewer bytes The numbers 0..127 require only 1 byte!). If the sample to be stored or transmitted has many small values, this alone can compress the data to 1/8 = 12.5% .The disadvantage is that large numbers (even with bits in the highest byte) will use more than 8 bytes. Whether it’s worth it depends on the heuristics of the sample.

X. The list goes on…


Can you live without knowing/using bitwise operators in Go (and many other programming languages)? The answer is yes. But if you know them, sometimes they can make your life easier and your programs more efficient.

If you want to learn more about this topic, read the Wikipedia article: Bitwise Operations and google the term “bitwise operators tutorial”, there are many good articles.

2> jcbwlkr..:


For what they are technically doing, check out the comments here

package main

 import "fmt"

 func main() {
     // Use bitwise OR | to get the bits that are in 1 OR 2
     // 1 = 00000001
     // 2 = 00000010
     // 1 | 2 = 00000011 = 3
     fmt.Println(1 | 2)

     // Use bitwise OR | to get the bits that are in 1 OR 5
     // 1 = 00000001
     // 5 = 00000101
     // 1 | 5 = 00000101 = 5
     fmt.Println(1 | 5)

     // Use bitwise XOR ^ to get the bits that are in 3 OR 6 BUT NOT BOTH
     // 3 = 00000011
     // 6 = 00000110
     // 3 ^ 6 = 00000101 = 5
     fmt.Println(3^6)

     // Use bitwise AND & to get the bits that are in 3 AND 6
     // 3 = 00000011
     // 6 = 00000110
     // 3 & 6 = 00000010 = 2
     fmt.Println(3 & 6)

     // Use bit clear AND NOT &^ to get the bits that are in 3 AND NOT 6 (order matters)
     // 3 = 00000011
     // 6 = 00000110
     // 3 &^ 6 = 00000001 = 1
     fmt.Println(3 &^ 6)
 }
 

View it on the playground

Please note that I gave two examples | to illustrate that it is not a true supplement 1 + 5.

As for practical uses I’m sure others can comment with more examples, but a common use is to create a flag bitmask for something like a permissions system.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/differences-between-some-operators-golang/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索