Bit-manipulation is confusing. not anymore!
Learn bitwise operator just by reading this-
A thread👇
✨Introduction
-> Bitwise Operations let you manipulate numbers at bit level. Bitwise operations are faster than regular operations.
There are main 6 bitwise operators:
1)Bitwise AND
2)Bitwise OR
3)Bitwise XOR
4)Bitwise left-shift
5)Bitwise right-shift
6)Bitwise complement
✨Bitwise AND
-> The Bitwise AND tests 2 binary numbers and returns bit values of 1 for positions where both numbers had a one, and a bit value of 0 at every other position.
Symbol- '&' (for C++, Java, Python, JavaScrpit)
✨Example of Bitwise AND
✨Bitwise OR
-> The Bitwise OR tests 2 binary numbers and returns a bit value of 0 for positions where both bits are 0, and a bit value of 1 at every other position.
Symbol- '|' (for C++, Java, Python, JavaScrpit)
✨Example of Bitwise OR
✨Bitwise XOR
-> The Bitwise XOR(Exclusive-OR) tests two binary numbers and returns bit values of 1 for positions where both bits are different; if they are the same then the result is 0.
Symbol- '^' (for C++, Java, Python, JavaScrpit)
✨Example of Bitwise XOR
✨Bitwise Complement
-> The Bitwise Complement(NOT) inverts the bits in a single binary number.
Symbol- '~' (for C++, Java, Python, JavaScrpit)
✨Bitwise Left-Shift
-> The Bitwise left shift moves all bits in the number to the left and fills vacated bit position with 0.
Overflow- if after the left shifting total number of bits exceeds the range, then MSB is discarded. This condition is called overflow.
✨More about Bitwise Left-Shift and example
-> if a number n is left-shifted I times then result in number x:
x=n*(2*i)
Caution:- This formula does not hold in case of an overflow condition.
✨Bitwise Right-Shift
-> The Bitwise right shift moves all the bits in the number to right and LSB gets discarded.
If the number is unsinged then vacated position gets filled by 0. It is always advised to not use the right shift in signed numbers.
✨More about Bitwise Right-Shift and example
-> if a number n is right-shifted I times then result in number x:
x=floor(n/(2*i))
I hope this thread was helpful. If you liked it make sure you like it and retweet the first tweet so that it can help others too.
Also, follow me
@Nihar_Thummar
About me
I'm Nihar 👋
I am a student and a developer.
I talk about
-🐍Python
-💻Data structures and algorithm
-🚀WEB 3
-🛠️Build in Public
If you found any mistakes please let me know! Also if you have any questions feel free to ask in comment !
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.