Contar números cuyo XOR con N es igual a OR con N

Dado un número N, la tarea es encontrar el conteo de X tal que N XOR X == N OR X , donde 0<=X<=N 
Ejemplos: 
 

Entrada : N = 5 
Salida : 2 
Para N = 5, 
5 XOR 2 == 5 O 2 
5 XOR 0 == 5 O 0 
Por lo tanto, la cuenta es 2.
Entrada : N = 7 
Salida : 1 
Para N = 7, 
7 XOR 0 == 7 O 0 
Por lo tanto, la cuenta es 1.
 

Enfoque: La idea es convertir un número dado a binario y luego contar los bits no configurados en él. 2^count nos da el número de X tal que N XOR X == N OR X .
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to find
// the XOR equals OR count
#include<iostream>
#include<math.h>
using namespace std;
 
class gfg {
     
    // Function to calculate count
    // of numbers with XOR equals OR
    public:
    int xorEqualsOrCount(int N)
    {
         
        // variable to store count of unset bits
        int count = 0;
        int bit;
        while (N > 0) {
             
            bit = N % 2;
            if (bit == 0)
                count++;
            N = N / 2;
        }
        return (int)pow(2, count);
    } };
 
    // Driver code
    int main()
    {
        gfg g ;
        int N = 7;
        cout<<g.xorEqualsOrCount(N);
        return 0;
    }
 
 
// This code is contributed by Soumik

Java

// Java program to find the XOR equals OR count
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to calculate count of numbers with XOR equals OR
    static int xorEqualsOrCount(int N)
    {
        // variable to store count of unset bits
        int count = 0;
        int bit;
        while (N > 0) {
            bit = N % 2;
            if (bit == 0)
                count++;
            N = N / 2;
        }
        return (int)Math.pow(2, count);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 7;
        System.out.println(xorEqualsOrCount(N));
    }
}

Python3

# Python3 program to find
# the XOR equals OR count
 
# Function to calculate count
# of numbers with XOR equals OR
def xorEqualsOrCount(N) :
 
    # variable to store
    # count of unset bits
    count = 0
 
    while(N > 0) :
 
        bit = N % 2
 
        if bit == 0 :
            count += 1
 
        N //= 2
 
    return int(pow(2, count))
 
# Driver code    
if __name__ == "__main__" :
 
    N = 7
    print(xorEqualsOrCount(N))
                 
# This code is contributed by
# ANKITRAI1

C#

// C# program to find
// the XOR equals OR count
using System;
 
class GFG {
 
    // Function to calculate count
    // of numbers with XOR equals OR
    static int xorEqualsOrCount(int N)
    {
         
        // variable to store count of unset bits
        int count = 0;
        int bit;
        while (N > 0) {
             
            bit = N % 2;
            if (bit == 0)
                count++;
            N = N / 2;
        }
        return (int)Math.Pow(2, count);
    }
 
    // Driver code
    public static void Main()
    {
        int N = 7;
        Console.WriteLine(xorEqualsOrCount(N));
    }
}
 
// This code is contributed by inder_verma..

PHP

<?php
// PHP program to find the XOR
// equals OR count
     
// Function to calculate count
// of numbers with XOR equals OR
function xorEqualsOrCount($N)
{
     
    // variable to store count
    // of unset bits
    $count = 0;
    while ($N > 0)
    {
        $bit = $N % 2;
        if ($bit == 0)
            $count++;
        $N = intval($N / 2);
    }
    return pow(2, $count);
}
 
// Driver code
$N = 7;
echo xorEqualsOrCount($N);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript program to find
// the XOR equals OR count
 
    // Function to calculate count
    // of numbers with XOR equals OR
    function xorEqualsOrCount(N)
    {
         
        // variable to store count of unset bits
        let count = 0;
        let bit;
        while (N > 0) {
             
            bit = N % 2;
            if (bit == 0)
                count++;
            N = parseInt(N / 2);
        }
        return Math.pow(2, count);
    }
 
    // Driver code
        let N = 7;
        document.write(xorEqualsOrCount(N));
 
</script>
Producción: 

1

 

Publicación traducida automáticamente

Artículo escrito por rachana soma 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 *