![]() |
VOOZH | about |
Given a number, change all bits at even positions to 0.
Examples:
Input : 30 Output : 10 Binary representation of 11110. Bits at Even positions are highlighted. After making all of them 0, we get 01010 Input : 10 Output : 10
Method 1 (Bit Traversal)
The idea is to traverse through all even bits. We accumulate all powers of 2 in a number to subtract. Finally we subtract the accumulated value from n to obtain the result.
10
Time Complexity - O(log n)
Space Complexity - O(1)
Method 2: (Bit masking)
10
Time Complexity - O(1)
Space Complexity - O(1)