How can I test if all bits are set or all bits are not?
问题 Using bitwise operator how can I test if the n least significant bits of an integer are either all sets or all not sets. For example if n = 3 I only care about the 3 least significant bits the test should return true for 0 and 7 and false for all other values between 0 and 7. Of course I could do if x = 0 or x = 7 , but I would prefer something using bitwise operators. Bonus points if the technique can be adapted to take into accounts all the bits defined by a mask. Clarification : If I