Bitwise Operations on Digits of a Number - GeeksforGeeks

14 Aug, 2021
Given a number N, the task is to perform the bitwise operations on digits of the given number N. The bitwise operations include:  
Finding the XOR of all digits of the given number N
Finding the OR of all digits of the given number N
Finding the AND of all digits of the given number N
Examples:  
Input: N = 486
Output: 
XOR = 10
OR = 14
AND = 0

Input: N = 123456
Output: 
XOR = 10
OR = 14
AND = 0
 
Approach: 
Get the number  Find the digits of the number and store it in an array for computation purpose.  Now perform the various bitwise operations (XOR, OR, and AND) on this array one by one.
Get the number  
Find the digits of the number and store it in an array for computation purpose.  
Now perform the various bitwise operations (XOR, OR, and AND) on this array one by one.
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
Javascript
// C++ implementation of the approach #include <bits/stdc++.h>using namespace std; int digit[100000]; // Function to find the digitsint findDigits(int n){    int count = 0;     while (n != 0) {         digit[count] = n % 10;        n = n / 10;        ++count;    }     return count;} // Function to Find OR// of all digits of a numberint OR_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {        // Find OR of all digits        ans = ans | digit[i];    }     // return OR of digits    return ans;} // Function to Find AND// of all digits of a numberint AND_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {        // Find AND of all digits        ans = ans & digit[i];    }     // return AND of digits    return ans;} // Function to Find XOR// of all digits of a numberint XOR_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {        // Find XOR of all digits        ans = ans ^ digit[i];    }     // return XOR of digits    return ans;} // Driver codevoid bitwise_operation(int N){     // Find and store all digits    int countOfDigit = findDigits(N);     // Find XOR of digits    cout << "XOR = "         << XOR_of_Digits(N, countOfDigit)         << endl;     // Find OR of digits    cout << "OR = "         << OR_of_Digits(N, countOfDigit)         << endl;     // Find AND of digits    cout << "AND = "         << AND_of_Digits(N, countOfDigit)         << endl;} // Driver codeint main(){     int N = 123456;     bitwise_operation(N);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{ static int []digit = new int[100000]; // Function to find the digitsstatic int findDigits(int n){    int count = 0;     while (n != 0) {         digit[count] = n % 10;        n = n / 10;        ++count;    }     return count;} // Function to Find OR// of all digits of a numberstatic int OR_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {                 // Find OR of all digits        ans = ans | digit[i];    }     // return OR of digits    return ans;} // Function to Find AND// of all digits of a numberstatic int AND_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {                 // Find AND of all digits        ans = ans & digit[i];    }     // return AND of digits    return ans;} // Function to Find XOR// of all digits of a numberstatic int XOR_of_Digits(int n, int count){     int ans = 0;     for (int i = 0; i < count; i++) {                 // Find XOR of all digits        ans = ans ^ digit[i];    }     // return XOR of digits    return ans;} // Driver codestatic void bitwise_operation(int N){     // Find and store all digits    int countOfDigit = findDigits(N);     // Find XOR of digits    System.out.print("XOR = "        + XOR_of_Digits(N, countOfDigit)        +"\n");     // Find OR of digits    System.out.print("OR = "        + OR_of_Digits(N, countOfDigit)        +"\n");     // Find AND of digits    System.out.print("AND = "        + AND_of_Digits(N, countOfDigit)        +"\n");} // Driver codepublic static void main(String[] args){     int N = 123456;     bitwise_operation(N);}} // This code is contributed by sapnasingh4991
# Python 3 implementation of the approachdigit = [0]*(100000) # Function to find the digitsdef findDigits(n):    count = 0     while (n != 0):         digit[count] = n % 10;        n = n // 10;        count += 1     return count # Function to Find OR# of all digits of a numberdef OR_of_Digits( n,count):    ans = 0     for i in range(count):                 # Find OR of all digits        ans = ans | digit[i]     # return OR of digits    return ans # Function to Find AND# of all digits of a numberdef AND_of_Digits(n, count):     ans = 0     for i in range(count):                 # Find AND of all digits        ans = ans & digit[i]     # return AND of digits    return ans # Function to Find XOR# of all digits of a numberdef XOR_of_Digits(n, count):     ans = 0     for i in range(count):                 # Find XOR of all digits        ans = ans ^ digit[i]     # return XOR of digits    return ans # Driver codedef bitwise_operation( N):     # Find and store all digits    countOfDigit = findDigits(N)     # Find XOR of digits    print("XOR = ",XOR_of_Digits(N, countOfDigit))     # Find OR of digits    print("OR = ",OR_of_Digits(N, countOfDigit))     # Find AND of digits    print("AND = ",AND_of_Digits(N, countOfDigit)) # Driver codeN = 123456;bitwise_operation(N) # This code is contributed by apurva raj
// C# implementation of the approachusing System; class GFG{  static int []digit = new int[100000];  // Function to find the digitsstatic int findDigits(int n){    int count = 0;      while (n != 0) {          digit[count] = n % 10;        n = n / 10;        ++count;    }      return count;}  // Function to Find OR// of all digits of a numberstatic int OR_of_Digits(int n, int count){      int ans = 0;      for (int i = 0; i < count; i++) {                  // Find OR of all digits        ans = ans | digit[i];    }      // return OR of digits    return ans;}  // Function to Find AND// of all digits of a numberstatic int AND_of_Digits(int n, int count){      int ans = 0;      for (int i = 0; i < count; i++) {                  // Find AND of all digits        ans = ans & digit[i];    }      // return AND of digits    return ans;}  // Function to Find XOR// of all digits of a numberstatic int XOR_of_Digits(int n, int count){      int ans = 0;      for (int i = 0; i < count; i++) {                  // Find XOR of all digits        ans = ans ^ digit[i];    }      // return XOR of digits    return ans;}  // Driver codestatic void bitwise_operation(int N){      // Find and store all digits    int countOfDigit = findDigits(N);      // Find XOR of digits    Console.Write("XOR = "        + XOR_of_Digits(N, countOfDigit)        +"\n");      // Find OR of digits    Console.Write("OR = "        + OR_of_Digits(N, countOfDigit)        +"\n");      // Find AND of digits    Console.Write("AND = "        + AND_of_Digits(N, countOfDigit)        +"\n");}  // Driver codepublic static void Main(String[] args){      int N = 123456;      bitwise_operation(N);}} // This code is contributed by 29AjayKumar
<script> // Javascript implementation of the approach let digit = [];   // Function to find the digitsfunction findDigits(n){    let count = 0;       while (n != 0) {           digit[count] = n % 10;        n = n / 10;        ++count;    }       return count;}   // Function to Find OR// of all digits of a numberfunction OR_of_Digits(n, count){       let ans = 0;       for (let i = 0; i < count; i++) {                   // Find OR of all digits        ans = ans | digit[i];    }       // return OR of digits    return ans;}   // Function to Find AND// of all digits of a numberfunction AND_of_Digits(n, count){       let ans = 0;       for (let i = 0; i < count; i++) {                   // Find AND of all digits        ans = ans & digit[i];    }       // return AND of digits    return ans;}   // Function to Find XOR// of all digits of a numberfunction XOR_of_Digits(n, count){       let ans = 0;       for (let i = 0; i < count; i++) {                   // Find XOR of all digits        ans = ans ^ digit[i];    }       // return XOR of digits    return ans;}   // Driver codefunction bitwise_operation(N){       // Find and store all digits    let countOfDigit = findDigits(N);       // Find XOR of digits    document.write("XOR = "        + XOR_of_Digits(N, countOfDigit)         + "<br/>");       // Find OR of digits    document.write("OR = "        + OR_of_Digits(N, countOfDigit)        + "<br/>");       // Find AND of digits    document.write("AND = "        + AND_of_Digits(N, countOfDigit)        + "<br/>");} // Driver Code     let N = 123456;       bitwise_operation(N); </script>
XOR = 7
OR = 7
AND = 0
 
Time Complexity: O(logN)Auxiliary Space: O(logN)
ApurvaRaj
sapnasingh4991
29AjayKumar
chinmoy1997pal
pankajsharmagfg
Algorithms-Bit Algorithms
Bitwise-AND
Bitwise-OR
Bitwise-XOR
Algorithms
Mathematical
School Programming
Write From Home
Mathematical
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
How to Start Learning DSA?
Difference between Algorithm, Pseudocode and Program
K means Clustering - Introduction
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete
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)
Coin Change | DP-7