Golang bit arithmetic

Source: Internet
Author: User
Tags bitwise bitwise operators
This is a creation in Article, where the information may have evolved or changed.

Http://blog.opskumu.com/golang-bitwise.html


Bit operation is a unary and two-dollar operation of the bitwise or binary number of the bit-mode in the program design. On many ancient microprocessors, bit operations are slightly faster than addition and subtraction, and the bitwise operations are much faster than the multiplication algorithm. In modern architectures, this is not the case: bit operations are usually the same as the addition operation (still faster than multiplication). Bitwise operators

First, Introduction

On the operation of bitwise operations, Wikipedia's theoretical introduction of bitwise operators is very clear. About Golang bit operation is similar to the C language, in addition to reverse operation and C language a little different, Golang use ^x , and C is to use ~x reverse.

Second, the concept

  • <<[Move left]
    • 1 << 2 = = 4
    • Output 0100, more common than the right shift, the rest of the post-shift blank 0
  • >>[Move right]
    • >> 2 = = 2
    • Output 0010
  • x ^ y[XOR]
    • 10 ^ 2 = = 8
    • The result of the operation is that if a bit is different then the bit is 1, otherwise the bit is 0
  • x | yOr
    • 10 | 2 = = 10
    • As long as one of the two corresponding binaries is 1, the result value of the bit is 1
  • x & yand
    • & 2 = = 2
    • Two corresponding binary is 1, the result value of this bit is 1, otherwise 0
  • ^x[Take the counter]
    • ^2 = =-3
    • Minus 1, reverse complement.

Third, reference

    • bit operator
    • golang Bit operation details
    • Tags:
    • Go 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.