Preface
Because I have been ignoring the bit operations in all languages, and I think it is not very useful, and it may be very simple to use, but in fact, I have been ignoring the use of them is still very useful. Let's first review the basics of bit operators.
Bit operator
With Operation:&
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Or operation:!
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 & 0 = 0
XOR:^
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
Move left: <<
1 << 10 = 1024
1 << 20 = 1M
1 << 30 = 1G
Move right:>>
1024 >> 10 = 1
1024 >>1 = 512
1024 >>2 = 256
An example of the above knowledge
Maybe this example is not particularly rigorous in practical use, but it also provides an idea for us to write code.
Here we take the privileges of Weibo or QQ users as an example:
A QQ account can use VIP membership, SVIP super membership, blue diamond user, yellow diamond user, red diamond user...
Our usual idea might be that if the database stores itself, it will store this field for the user to indicate which privileges the user has enabled.
Our writing method in the code may also be as follows:
package main import ( "fmt" ) type users struct { name string flag uint8 vip bool svip bool blue bool red bool yellow bool } func setVip(user users) users { = true return user } func isVip(user users) { if { ("user is vip") } else { ("user is not vip") } } func binaryTest() { var user users = "test01" = true isVip(user) = false isVip(user) } func main() { binaryTest() }
This implementation method is also OK, but obviously we need to do operations for each type, and if there are more and more members and drills in the future, it is not the best method. Let's implement the above functions through bit operations, and the code is as follows:
package main import ( "fmt" ) type users struct { name string flag uint8 } // Here, the default is 00000 by displacement. From the left, it is vip, svip, blue, red, yellow.const ( vip = 1 svip = (1 << 1) blue = (1 << 2) red = (1 << 3) yello = (1 << 4) ) // setFlag is used to set what privileges the user has enabledfunc setFlag(user users, isSet bool, typeFlag uint8) users { if isSet == true { = | typeFlag } else { = ^ typeFlag } return user } //isFlag is used to determine whether the user activates a certain privilegefunc isFlag(user users, typeFlag uint8) bool { result := & typeFlag return result == typeFlag } func binaryTest() { var user users = "coder" = 0 //Discern whether the user is a VIP result := isFlag(user, vip) ("user is Vip:%t\n", result) //Activate vip for users and see if the user activates vip or not user = setFlag(user, true, vip) result = isFlag(user, vip) ("user is Vip:%t\n", result) //Cancel the user's vip and check whether the user is still vip user = setFlag(user, false, vip) result = isFlag(user, vip) ("user is Vip:%t\n", result) } func main() { binaryTest() }
The above code is a very clever way to use bit operations. When adding various privileges, you only need to add a line of code to the initially defined constant light, so that you can directly set and cancel this privilege and check whether it is enabled.
Attached the common >> Right shift <<Left shift It feels like right shift left shift should also be very common to use
Continue to see the example:
package main import "fmt" func main() { x := 2 y := 4 (x<<1) (y>>1) }output:4 2
Convert to binary and move left or right.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.