📌  相关文章
📜  检查一个数字是否具有相同的置位和未置位位数

📅  最后修改于: 2021-05-05 03:23:08             🧑  作者: Mango

给定数字N,任务是检查给定数字中的置1和未置1位数是否相同。

例子:

Input: 12
Output: Yes
1100 is the binary representation of 12
which has 2 set and 2 unset bits 

Input: 14
Output: No

方法:遍历给定数字的二进制表示形式,使用n&1检查是否设置了最左边的位。如果n&1返回1,则设置最左边的位。正确,每次将数字移位1即可检查下一位。二进制表示形式完全遍历后,检查设置位和未设置位的数量是否相同。如果它们相同,则打印“是”,否则打印“否”。

下面是上述方法的实现:

C++
// C++ program to check if a number
// has same number of set and unset bits
#include 
using namespace std;
  
// Function to check if a number
// has same setbits and unset bits
bool checkSame(int n)
{
    int set = 0, unset = 0;
  
    // iterate for all bits of a number
    while (n) {
  
        // if set
        if (n & 1)
            set++;
        // if unset
        else
            unset++;
  
        // right shift number by 1
        n = n >> 1;
    }
  
    // is number of set bits are
    // equal to unset bits
    if (set == unset)
        return true;
    else
        return false;
}
  
// Driver Code
int main()
{
    int n = 12;
  
    // function to check
    if (checkSame(n))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


Java
// Java program to check if 
// a number has same number 
// of set and unset bits
import java.io.*;
  
class GFG 
{
      
// Function to check if a 
// number has same setbits 
// and unset bits
static boolean checkSame(int n)
{
    int set = 0;
    int unset = 0;
  
    // iterate for all
    // bits of a number
    while (n > 0) 
    {
  
        // if set
        if ((n & 1) ==  1)
            set++;
              
        // if unset
        else
            unset++;
  
        // right shift 
        // number by 1
        n = n >> 1;
    }
  
    // is number of set bits 
    // are equal to unset bits
    if (set == unset)
        return true;
    else
        return false;
}
  
// Driver Code
public static void main (String[] args) 
{
int n = 12;
  
// function to check
if (checkSame(n))
    System.out.println ("YES");
else
    System.out.println("NO");
      
}
}
  
// This code is contributed
// by akt_mit


Python3
# Python program to check if a number
# has same number of set and unset bits
  
# Function to check if a number
# has same setbits and unset bits
def checkSame(n):
    set, unset = 0, 0
  
    # iterate for all bits of a number
    while(n):
  
        # if set
        if (n and 1):
            set + 1
              
        # if unset
        else:
            unset += 1
  
        # right shift number by 1
        n = n >> 1
          
    # is number of set bits are
    # equal to unset bits
    if (set == unset):
        return True
    else:
        return False
  
# Driver Code
if __name__ == '__main__':
    n = 12
  
    # function to check
    if (checkSame(n)):
        print("YES")
    else:
        print("NO")
  
# This code is contributed 
# by 29AjayKumar


C#
// C# program to check if 
// a number has same number 
// of set and unset bits
using System;
  
public class GFG{
          
// Function to check if a 
// number has same setbits 
// and unset bits 
static bool checkSame(int n) 
{ 
    int set = 0; 
    int unset = 0; 
  
    // iterate for all 
    // bits of a number 
    while (n > 0) 
    { 
  
        // if set 
        if ((n & 1) == 1) 
            set++; 
              
        // if unset 
        else
            unset++; 
  
        // right shift 
        // number by 1 
        n = n >> 1; 
    } 
  
    // is number of set bits 
    // are equal to unset bits 
    if (set == unset) 
        return true; 
    else
        return false; 
} 
  
// Driver Code 
      
    static public void Main (){
        int n = 12; 
  
    // function to check 
    if (checkSame(n)) 
        Console.WriteLine("YES"); 
    else
        Console.WriteLine("NO"); 
      
    } 
}


PHP
> 1; 
    } 
  
    // is number of set bits are 
    // equal to unset bits 
    if ($set == $unset) 
        return true; 
    else
        return false; 
} 
  
// Driver Code 
$n = 12; 
  
// function to check 
if (checkSame($n)) 
    echo "YES"; 
else
    echo "NO"; 
  
// This code is contributed
// by Sach_Code
?>


输出:
YES