✨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.
Real-life applications of Various Data Structures.
A thread👇
✨ STACK 1) UNDO option
2) Text editor, you push letter by letter to the stack so you erase back.
3) Recursion (in built stack)
✨QUEUE 1) Your browser deletes the history past one month.
2) All the pictures you delete from your phone will be stored in a queue, so that after sometimes front items can be popped out easily (permanently deleted)
3) Waiting list: Registration Requests are put in the queue.