Bitwise NOT operator in the programming world usually takes one number and returns the inverted bits of that number as shown below:
Bitwise NOT of 1 = 0 Bitwise NOT of 0 = 1
Input : X = 010101 Output : Bitwise NOT of X = 101010
But Golang doesn’t have any specified unary Bitwise NOT(~) or you can say Bitwise Complement operator like other programming languages(C/C++, Java, Python, etc). Here, you have to use Bitwise XOR(^) operator as Bitwise NOT operator. But how?
Let’s understand how Bitwise XOR takes in any two equal length bit patterns and performs Exclusive OR operation on each pair of corresponding bits.
1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 0 = 0 0 XOR 1 = 1
Here, you can see the result of XOR(M, N) = 1 only if M != N else it will be 0. So here, we will use the XOR operator as a unary operator to implement the one’s complement to a number.
In Golang, suppose you have a given bit M, so ^M = 1 ^ M which will be equal to one’s complement or you can say the Bitwise NOT operator result.
Example: Suppose you have the given bits as 010101.
Input: 11111111 XOR 00001111 Output: 11110000
Here, you can see, if we simply solve the Bitwise Not of 00001111 then it will be equal to 11110000.
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Ternary operator vs Null coalescing operator in PHP
- Difference between var keyword and short declaration operator in Golang
- How to instantiate Struct Pointer Address Operator in Golang?
- Double not (!!) operator in PHP
- Check If the Rune is a Unicode Punctuation Character or not in Golang
- Check If the Rune is a Letter or not in Golang
- Check If the Rune is a Decimal Digit or not in Golang
- Check If the Rune is an Uppercase Letter or not in Golang
- Check If the Rune is a Lowercase Letter or not in Golang
- Check If the Rune is a Symbolic Character or not in Golang
- Check If the Rune is a Space Character or not in Golang
- Checking the Given File Exists or Not in Golang
- Checking if structure is empty or not in Golang
- How to check pointer or interface is nil or not in Golang?
- How to hide “Image not found” icon when source image is not found?
- unordered_multiset operator = in C++ STL
- unordered_set operator= in C++ STL
- 'AND' vs '&&' as operator in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.