📜  将数字中的所有偶数位更改为0

📅  最后修改于: 2021-05-25 10:34:37             🧑  作者: Mango

给定一个数字,将偶数位置的所有位都更改为0。

例子:

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

方法1(位遍历)

这个想法是遍历所有偶数位。我们将一个数字的所有2的幂累加起来减去。最后,我们从n中减去累加值以获得结果。

C++
// C++ program to change even
// bits to 0.
#include 
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 code
int main()
{
    int n = 30;
    cout << changeEvenBits(n) << endl;
 
    return 0;
}


Java
// 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
# 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 code
n = 30
print changeEvenBits(n)
 
# This code is contributed by Sachin Bisht


C#
// 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
>= 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
?>


Javascript


C++
#include 
using namespace std;
 
int convertEvenBitToOne(int n) { return (n & 0xaaaaaaaa); }
 
int main()
{
    int n = 30;
    cout << convertEvenBitToOne(n);
    return 0;
}


Java
// Java program using Bitmask to
// Change all even bits in a
// number to 0
import 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.


Python
def convertEvenBitToOne(n):
    return (n & 0xaaaaaaaa)
 
 
# Driver code
n = 30
print convertEvenBitToOne(n)
 
# This code is contributed by Sachin Bisht


C#
// C# program using Bitmask to
// Change all even bits in a
// number to 0
using 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


Javascript


输出
10

方法2 :(位屏蔽)

C++

#include 
using namespace std;
 
int convertEvenBitToOne(int n) { return (n & 0xaaaaaaaa); }
 
int main()
{
    int n = 30;
    cout << convertEvenBitToOne(n);
    return 0;
}

Java

// Java program using Bitmask to
// Change all even bits in a
// number to 0
import 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.

Python

def convertEvenBitToOne(n):
    return (n & 0xaaaaaaaa)
 
 
# Driver code
n = 30
print convertEvenBitToOne(n)
 
# This code is contributed by Sachin Bisht

C#

// C# program using Bitmask to
// Change all even bits in a
// number to 0
using 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


Java脚本


输出
10