'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 1:[1]

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.

Solution 2:[2]

You can create a quick String image like so:

for i in 1...25 {
    var foo = ""
    for j in 1...25 {
        foo += i&j >= i^j ? "•" : " "
    }
    print(foo)
}
•                        
 ••                      
 ••                      
   ••••                  
   ••••                  
   ••••                  
   ••••                  
       ••••••••          
       ••••••••          
       ••••••••          
       ••••••••          
       ••••••••          
       ••••••••          
       ••••••••          
       ••••••••          
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••
               ••••••••••

Notice how the squares are growing quadratically.

From this you can see that when i == j == true it always returns true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Damien_The_Unbeliever
Solution 2 0-1