In this post i'm going to show you some bit operations with &, |, <<, >>, ~ using Qt example. This example include operations like set a particular bit in the given number, clear bit, check bit is set or not and to check whether the number is even or odd.
Following demonstrates the psuedo code of the operations.
> To Set a bit in the given number : perform Betwise OR (|) operation with 1 left shift(<<) by index of the bit to set
>To clear a bit in the given number: perform Betwise AND (&) operation with the inverted(~) 1 left shift(<<) by index of the bit to clear
Following demonstrates the psuedo code of the operations.
> To Set a bit in the given number : perform Betwise OR (|) operation with 1 left shift(<<) by index of the bit to set
num = num | (1<<index);
>To clear a bit in the given number: perform Betwise AND (&) operation with the inverted(~) 1 left shift(<<) by index of the bit to clear
num = num & ~(1<<index);
