Golang bit operation

Source: Internet
Author: User
Tags bitwise operators
This is a creation in Article, where the information may have evolved or changed.
package main import  (     "FMT")  //  get all even func even between 0-n (a  int)   (array []int)  {    for i := 0; i <  a; i++ {        if i&1 == 0 {  //  bitwise operators & As used in C language              Array = append (array, i)         }     }    return array} //  swaps the value of two variables//  does not need to use the third variable to do the intermediate variable Func swap (a ,  b int)   (int, int)  {    a ^= b //  xor equals operation     b ^= a    a ^= b    return  a, b} //  left shift, right shift operation Func shifting (A int)  int {    a  = a << 1    a = a >> 1    return a}    Transform symbol func nagation (a int)  int {        //   NOTE:  c language is  ~a+1 this way     return ^a + 1 //  The go language is different from the C language, the go language does not support the ~ symbol. } func main ()  {    fmt. Printf ("even: %v\n",  even (+))     a, b := swap (100, 200)     fmt. Printf ("swap: %d\t%d\n",  a, b)     fmt. Printf ("shifting: %d\n",  shifting (+))     fmt. Printf ("nagation: %d\n",  nagation (100))}
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.