Unset the rightmost set bit [duplicate]
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: How do you set, clear and toggle a single bit in C? Removing lowest order bit n is a positive integer. How can its rightmost set bit be unset? Say n = 7 => n = 0111. I want 0110 as the output. Is there any simple bitwise hack to achieve the goal? 回答1: Try n & (n-1) where & is bitwise AND n = 7 n - 1 =6 n & (n-1)=> 0 1 1 1 (7) & 0 1 1 0 (6) --------- 0 1 1 0 (done!) EDIT (in response to the comment given by