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.
1 <variable> & <variable>
2 <value> & <value>
3 <variable> & <value>
1 a := 5
2 b := 1
3 a & b;
1 package main
2
3 import "fmt"
4
5 func main() {
6
7 a, b := 5, 4
8
9
10
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).
1 Bitwise AND operation : 4
2
3 Example
4 ------------
5
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
1 package main
2
3 import "fmt"
4
5 func main() {
6
7 a, b := 5, 1
8
9
10
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.
1 Bitwise AND operation : 1
Related options for your search