Change all even bits in a number to 0 | C ++ program to change even bits to 0. ; Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int changeEvenBits ( int n ) { int to_subtract = 0 ; int m = 0 ; for ( int x = n ; x ; x >>= 2 ) { if ( x & 1 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } int main ( ) { int n = 30 ; cout << changeEvenBits ( n ) << endl ; return 0 ; }