VOOZH about

URL: https://www.geeksforgeeks.org/dsa/change-even-bits-number-0/

⇱ Change all even bits in a number to 0 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Change all even bits in a number to 0

Last Updated : 22 Jun, 2022

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. 


Output
10

Time Complexity - O(log n)

Space Complexity - O(1)

Method 2: (Bit masking)


Output
10

Time Complexity - O(1)

Space Complexity - O(1)


 

Comment
Article Tags: