Bitwise Operations on Digits of a Number | C ++ implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit [ 100000 ] ; int findDigits ( int n ) { int count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } int OR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } int AND_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } int XOR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } void bitwise_operation ( int N ) { int countOfDigit = findDigits ( N ) ; cout << " XOR ▁ = ▁ " << XOR_of_Digits ( N , countOfDigit ) << endl ; cout << " OR ▁ = ▁ " << OR_of_Digits ( N , countOfDigit ) << endl ; cout << " AND ▁ = ▁ " << AND_of_Digits ( N , countOfDigit ) << endl ; } int main ( ) { int N = 123456 ; bitwise_operation ( N ) ; return 0 ; }