SoFunction
Updated on 2025-03-05

A brief discussion on the common operations of golang binary bit bit

As a popular popular language that takes into account performance and efficiency, I believe many people know that it has always been very eye-catching in the programming language rankings. As a strongly typed language, the operation of binary bits is definitely inevitable. The smallest unit of data is bits, especially in the network, it is used to encapsulate, unpack, read binary files, etc., etc.

Therefore, it is still necessary to learn the common operations of golang binary bits well. Moreover, many operations, especially multiplication and division, have very low CPU efficiency. At this time, binary operations can be replaced. I won’t say much, just add some information!

package main 
import (
  "fmt"
  "/imroc/biu"
)
 
/**
   Common operations of golang binary bit bits, biu is a library for converting binary displays
   mengdj@
  */
func main() {
  var(
    /**
     1 byte = 8 binary bits, each data type takes up different bytes
     Please note that bit operations must not exceed the boundary. If a certain type occupies 8 bits, do not exceed this range when offset.
      */
    a uint8=30
  )
  //a output result: 00011110  ((a))
  /**
   Set a certain bit to 1, for example, set the 8th bit, and the number from right to left needs to be offset by 7 bits, be careful not to cross the bounds
   1<<7=1000 0000 Then the 8th bit after offset from a logic or | is 1. When the logic| operation is used, it is true and it is true.
    */
   b:=a|(1&lt;&lt;7)
   //b output result: 10011110   ((b))
   /**
    Set a certain bit to 0, for example, set the 4th bit, and the number from right to left needs to be offset by 3 bits, be careful not to cross the bounds
    1<<3=0000 1000 Then invert to get 1111 0111 Then logic&a
    */
   c:=a&amp;^(1&lt;&lt;3)
   //c output result: 00010110   ((c))
   /**
    Get the value of a certain bit, that is, move the value of a certain bit to the first bit by shifting left and right, of course, you can also obtain it through calculation.
    If you get the 4th position of a
    First remove the value of more than 4 digits a<<4=1110 0000, then remove the 3 digits on the right a>>7=0000 0001
     */
    d:=(a&lt;&lt;4)&gt;&gt;7
    //d output result: 00000001 i.e. 1    ((d))
    /**
     Invert a certain position, that is, change 1 of a certain position to 0, 0 to 1
     Here, the or operator ^ is used. The bit value is the same as 0, and the difference is 1.
     If you get the 4th bit of a, 1<<3=0000 1000
     0000 1000 ^ 0001 1110 = 0001 0110
     */
    e:=a^(1&lt;&lt;3)
    //d output result: 00010110 i.e. 1    ((e))
 
    /**
     The last one is a comprehensive usage. If the tcp protocol requires the client to send a handshake packet first, the packet occupies 1 byte, the first 2 bits of reserved fields must be 0, the middle 3 bits of the client require the server version, and the last 1 bit of the client version
     Assuming that our server version requirements and our own version are both 3, how should we build this package? Target 0001 1011
     Many language types do not have direct bit units, as long as bytes need to be obtained through other methods. In fact, it is simple | or operation plus offset. It is worth noting that the network uses big-endian bytes and needs to be converted before transmission.
     rf=0 0000 0000
     svf=3 0000 0011 Offset 3 bits to get 0001 1000
     cvf=3 0000 0011
     calculate
     0000 0000
     |
     0001 1000
     |
     0000 0011
     =
     0001 1011
     */
    var rf,svf,cvf uint8=0,3,3
    head:=rf|(svf&lt;&lt;3)|cvf
    //head output result: 00011011    ((head))
}

Supplement: Go language binary installation

The binary installation method of Go language is very simple.

1. Download the /dl/ binary installation package here

2. Unzip the installation package to the /usr/local/go directory

3. Edit /etc/profile and add export PATH=$PATH:/usr/local/go/bin

4. source /etc/profile, do not log out and make the profile take effect

5. Create file to test whether the installation is successful

package main
import "fmt"
func main() {
  ("hello, world\n")
}

6. Run

[root@ffe406bc816b my]# go run

hello, world

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.