Bitwise AND (&) operator in Golang
The bitwise AND operator is binary operator, perform AND operation with Left side operand and right side operand. It perform operation on each bit of binary number. If the number is decimal it converts the binary and perform AND operation. It compares each bit of both operand and if it founds 1 at same position of each operand, it consider 1 otherwise 0.
Syntax
 1 <variable> & <variable>
 2 <value> & <value>
 3 <variable> & <value>
Bitwise AND
 1 a := 5
 2 b := 1
 3 a & b; // output 1
Bitwise AND
 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6 
 7 	a, b := 5, 4
 8 	// 1 & 1 = 1
 9 	// 1 & 0 = 0
 10 	// 101 & 100 = 100 = 4
 11 	fmt.Printf("Bitwise AND operation : %d", a&b)
 12 }
 13 
In the above example, we have define two variables with value 5 and 4 respectively and performing bitwise AND operation. The binary value for each number 101 and 100 respectively. The result of AND operation will be 100 (integer value will be 4).
Output
 1 Bitwise AND operation : 4
 2 
 3 Example
 4 ------------
 5 // 8 bit system generate 8 bit binary number otherwise 16 digit
 6 5 = 00000101 (In Binary)
 7 4 = 00000100 (In Binary)
 8 
 9 Bitwise AND operation of 5 and 4
 10   00000101
 11 & 00000100
 12   ________
 13   00000100 = 4 (In decimal)

Example 2

Bitwise AND (&)
 1 package main
 2 
 3 import "fmt"
 4 
 5 func main() {
 6 
 7 	a, b := 5, 1
 8 	// 1 & 1 = 1
 9 	// 1 & 0 = 0
 10 	// 101 & 001 = 001 = 1
 11 	fmt.Printf("Bitwise AND operation : %d", a&b)
 12 }
In the above example, an integer variable declared and initalized with value 5 and 1 respectively. A bitwise AND operation is performed which will print 1, as it compares each bits of the respective numbers and consider 1 where both variables respective bit value is 1, otherwise considered as 0.
Output
 1 Bitwise AND operation : 1
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us