bit-manipulationbitwise-operatorsbitwise-xor

When bitwise and & operation is greater or equal to bitwise xor ^ operation?


When bitwise & operation is greater or equal to bitwise ^ operation?

For example, 4&7 >= 4^7 is true.

How do we know when it will be true?

The one way is to do manually what will be hidden trick?


Solution

  • If we just talk about the set bits. This condition will be true when the highest common set bit is also the highest set bit in each of the numbers.

    Why? Because when the highest set bit in both numbers is also a common bit, then that bit will be set in the result of the AND operation. And it will not be set in the result of the XOR operation.

    And if the highest set bit in the largest number is not also set in the lower number, then necessarily that bit will be set in the XOR operation and not set in the AND operation.