Bitwise Operators

Truth Table X Y X & Y X | Y X ^ Y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Points to Remember The left-shift and right-shift operators should not be used for negative numbers Left Shift(<<) just means multiply by 2. Similarly >> results division by 2. XOR results 0 if both bits are same. So a^1=~a , a^0=a and a^a=0. 1’s and 2’s Complement 1’s complement means negation(not) of any number get 2’s complement by adding 1 to 1’s complement Original number: 0000 0101 (5) 1's complement: 1111 1010 Add 1 2's complement: 1111 1011 2's complement = (~binary number) + 1 Questions Q1. How to toggle or flip a particular bit in a number? To toggle any bit in a variable, Use (^) exclusive OR operator. ...

February 19, 2025 · 2 min