Comprobar si un número tiene el mismo número de bits activados y desactivados

Dado un número N, la tarea es comprobar si el recuento de bits activados y desactivados en el número dado es el mismo. 
Ejemplos: 
 

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

Input: 14
Output: No

Enfoque: Recorra la representación binaria del número dado, verifique si el bit más a la izquierda está configurado o no usando n & 1 . Si n & 1 devuelve 1, entonces se establece el bit más a la izquierda. Correcto, cambie el número cada vez por 1 para verificar el siguiente bit. Una vez que la representación binaria se recorre por completo, compruebe si el número de bits activados y desactivados es el mismo. Si son iguales, escriba «SÍ», de lo contrario, escriba «NO». 
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to check if a number
// has same number of set and unset bits
#include <bits/stdc++.h>
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

<?php
// PHP 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
function checkSame($n)
{
    $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
$n = 12;
 
// function to check
if (checkSame($n))
    echo "YES";
else
    echo "NO";
 
// This code is contributed
// by Sach_Code
?>

Javascript

<script>
 
// Javascript 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
function checkSame( n)
{
    let 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 
    let n = 12;
 
    // function to check
    if (checkSame(n))
        document.write("YES");
    else
       document.write("NO");
     
  // This code is contributed by jana_sayantan. 
</script>
Producción: 

YES

 

Publicación traducida automáticamente

Artículo escrito por Mohd_Saliem y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *