Bitwise Operations on Digits of a Number Last Updated : 14 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 NFinding the OR of all digits of the given number NFinding 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. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; int digit[100000]; // Function to find the digits 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 number 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 number 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 number 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 code void 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 code int main() { int N = 123456; bitwise_operation(N); return 0; } Java // Java implementation of the approach import java.util.*; class GFG{ static int []digit = new int[100000]; // Function to find the digits static 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 number static 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 number static 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 number static 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 code static 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 code public static void main(String[] args) { int N = 123456; bitwise_operation(N); } } // This code is contributed by sapnasingh4991 Python 3 # Python 3 implementation of the approach digit = [0]*(100000) # Function to find the digits def 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 number def 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 number def 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 number def 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 code def 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 code N = 123456; bitwise_operation(N) # This code is contributed by apurva raj C# // C# implementation of the approach using System; class GFG{ static int []digit = new int[100000]; // Function to find the digits static 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 number static 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 number static 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 number static 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 code static 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 code public static void Main(String[] args) { int N = 123456; bitwise_operation(N); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of the approach let digit = []; // Function to find the digits function 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 number function 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 number function 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 number function 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 code function 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> Output: XOR = 7 OR = 7 AND = 0 Time Complexity: O(logN)Auxiliary Space: O(logN) Comment More infoAdvertise with us Next Article Bitwise Operations on Digits of a Number S srijan_de Follow Improve Article Tags : Algorithms Mathematical Computer Science Fundamentals Write From Home DSA Bitwise-XOR Algorithms-Bit Algorithms Bitwise-OR Bitwise-AND +5 More Practice Tags : AlgorithmsMathematical Similar Reads Arithmetic Operations of Octal Numbers Generally, Octal Number representation is expressed by subscript 8 or octal (o). It has a base of 8 digits ranging from 0 to 7 (i.e., 0, 1, 2, 3, 4, 5, 6, 7). Arithmetic operations of octal numbers are also performed the same as decimal or binary arithmetic operations. The addition table for octal n 1 min read Arithmetic Operations of Binary Numbers Binary is a base-2 number system that uses two states 0 and 1 to represent a number. We can also call it to be a true state and a false state. A binary number is built the same way as we build a normal decimal number. Binary arithmetic is an essential part of various digital systems. You can add, su 1 min read Sum of Digits of a Number Given a number n, find the sum of its digits.Examples : Input: n = 687Output: 21Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12Output: 3Explanation: The sum of its digits are: 1 + 2 = 3Table of Content[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space[Approach 2] Using 6 min read Program to calculate product of digits of a number Given a number, the task is to find the product of the digits of a number. Examples: Input: n = 4513 Output: 60 Input: n = 5249 Output: 360 General Algorithm for product of digits in a given number: Get the numberDeclare a variable to store the product and set it to 1Repeat the next two steps till t 7 min read Count unset bits of a number Given a number n, count unset bits after MSB (Most Significant Bit).Examples : Input : 17 Output : 3 Binary of 17 is 10001 so unset bit is 3 Input : 7 Output : 0 A Simple Solution is to traverse through all bits and count unset bits. C++ // C++ program to count unset bits in an integer #include < 7 min read Like