Change all even bits in a number to 0 - GeeksforGeeks

31 Jan, 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. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to change even// bits to 0.#include <bits/stdc++.h>using namespace std; // Returns modified number with// all even bits 0.int changeEvenBits(int n){    // To store sum of bits    // at even positions.    int to_subtract = 0;     // To store bits to shift    int m = 0;     // One by one put all even    // bits to end    for (int x = n; x; x >>= 2) {        // If current last bit        // is set, add it to ans        if (x & 1)            to_subtract += (1 << m);         // Next shift position        m += 2;    }     return n - to_subtract;} // Driver codeint main(){    int n = 30;    cout << changeEvenBits(n) << endl;     return 0;}
// Java program to change even// bits to 0.import java.util.*;class GFG {    // Returns modified number with    // all even bits 0.    static int changeEvenBits(int n)    {        // To store sum of bits        // at even positions.        int to_subtract = 0;         // To store bits to shift        int m = 0;         // One by one put all even        // bits to end        for (int x = n; x > 0; x >>= 2) {            // If current last bit            // is set, add it to ans            if ((x & 1) > 0)                to_subtract += (1 << m);             // Next shift position            m += 2;        }         return n - to_subtract;    }     // Driver code    public static void main(String[] args)    {        int n = 30;        System.out.println(changeEvenBits(n));    }}/* This code is contributed by Mr. Somesh Awasthi */
# Python program to change even# bits to 0. # Returns modified number with# all even bits 0.  def changeEvenBits(n):     # To store sum of bits    # at even positions.    to_subtract = 0     # To store bits to shift    m = 0     # One by one put all even    # bits to end    x = n    while(x):         # If current last bit        # is set, add it to ans        if (x & 1):            to_subtract += (1 << m)         # Next shift position        m += 2        x >>= 2     return n - to_subtract  # Driver coden = 30print(changeEvenBits(n)) # This code is contributed by Sachin Bisht
// C# program to change even// bits to 0.using System; class GFG {         // Returns modified number with    // all even bits 0.    static int changeEvenBits(int n)    {                 // To store sum of bits        // at even positions.        int to_subtract = 0;             // To store bits to shift        int m = 0;             // One by one put all even        // bits to end        for (int x = n; x > 0; x >>= 2)        {                         // If current last bit            // is set, add it to ans            if ((x & 1) > 0)            to_subtract += (1 << m);                 // Next shift position            m += 2;        }             return n - to_subtract;    }         // Driver code    public static void Main()    {        int n = 30;                 Console.Write(changeEvenBits(n));    }} // This code is contributed by nitin mittal.
<?php// PHP program to change even// bits to 0. // Returns modified number with// all even bits 0.function changeEvenBits($n){         // To store sum of bits    // at even positions.    $to_subtract = 0;     // To store bits to shift    $m = 0;     // One by one put all even    // bits to end    for ($x = $n; $x; $x >>= 2)    {                 // If current last bit        // is set, add it to ans        if ($x & 1)        $to_subtract += (1 << $m);         // Next shift position        $m += 2;    }     return $n - $to_subtract;}     // Driver code    $n = 30;    echo changeEvenBits($n) ; // This code is contributed by nitin mittal?>
<script> // js program to change even// bits to 0. // Returns modified number with// all even bits 0.function changeEvenBits(n){         // To store sum of bits    // at even positions.    let to_subtract = 0;     // To store bits to shift    let m = 0;     // One by one put all even    // bits to end    for (x = n; x; x >>= 2)    {                 // If current last bit        // is set, add it to ans        if (x & 1)        to_subtract += (1 << m);         // Next shift position        m += 2;    }     return n - to_subtract;}     // Driver code    n = 30;    document.write( changeEvenBits(n) ); // This code is contributed by sravan kumar </script>
10
Method 2: (Bit masking)
C++
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; int convertEvenBitToOne(int n) { return (n & 0xaaaaaaaa); } int main(){    int n = 30;    cout << convertEvenBitToOne(n);    return 0;}
// Java program using Bitmask to// Change all even bits in a// number to 0import java.io.*; class GFG {     static int convertEvenBitToOne(int n)    {        return (n & 0xaaaaaaaa);    }     // Driver code    public static void main(String[] args)    {        int n = 30;        System.out.println(convertEvenBitToOne(n));    }} // This code is contributed by anuj_67.
def convertEvenBitToOne(n):    return (n & 0xaaaaaaaa)  # Driver coden = 30print(convertEvenBitToOne(n)) # This code is contributed by Sachin Bisht
// C# program using Bitmask to// Change all even bits in a// number to 0using System; class GFG {     static long convertEvenBitToOne(int n)    {        return (n & 0xaaaaaaaa);    }     // Driver code    public static void Main()    {        int n = 30;        Console.WriteLine(convertEvenBitToOne(n));    }} // This code is contributed by anuj_67.
<?php// PHP program using Bitmask to// Change all even bits in a// number to 0 function convertEvenBitToOne($n){    return ($n & 0xaaaaaaaa);} // Driver Code$n = 30;echo convertEvenBitToOne($n); // This code is contributed by anuj_67.?>
<script> // java script program using Bitmask to// Change all even bits in a// number to 0 function convertEvenBitToOne(n){    return (n & 0xaaaaaaaa);} // Driver Coden = 30;document.write(convertEvenBitToOne(n));// This code is contributed by sravan kumar </script>
10
This article is contributed by RISHABH RAJ JHA . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
vt_m
mayanktyagi1709
sravankumar8128
amartyaghoshgfg
Samsung
Bit Magic
Mathematical
Samsung
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Bit Fields in C
Bits manipulation (Important tactics)
Add two numbers without using arithmetic operators
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers