Menu Close

Go – How to use Bitwise NOT operator in Go

Here, we will see how to use Bitwise NOT operator in Go. There is no bitwise NOT operator in go like we use caret symbol (^) in C. Similar to that, we can use AND NOT (&^) operator in go which is used to clear the bit.

&^ is binary ‘and not’ operator.

Example:

1:) a = 1010

You want to clear bit at 2nd position 
from right side.

a = a &^ (1 << 1)

a = 8 which is equivalent to (1000)

2:) a = 1110

You want to clear bit at 3nd position
from right side.

a = a &^ (1 << 2)

a = 1010

Code:

package main

import (

 "fmt"

)

func main() {

  a := 10

  a = a &^ (1 << 1)

  fmt.Println(a)

  a = 14

  a = a &^ (1 << 2)

  fmt.Println(a)

}

Output:

8

10

To learn more about golang. Please follow given below link.

https://www.techieindoor.com/go-lang/

References:

https://golang.org/doc/
https://golang.org/pkg/

Posted in golang, operator

Leave a Reply

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