Bitwise Operations on Digits of a Number | Javascript 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
let digit = [ ] ; function findDigits ( n ) { let count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } function OR_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } function AND_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } function XOR_of_Digits ( n , count ) { let ans = 0 ; for ( let i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } function bitwise_operation ( N ) { let countOfDigit = findDigits ( N ) ; document . write ( " " + XOR_of_Digits ( N , countOfDigit ) + " " ) ; document . write ( " " + OR_of_Digits ( N , countOfDigit ) + " " ) ; document . write ( " " + AND_of_Digits ( N , countOfDigit ) + " " ) ; } let N = 123456 ; bitwise_operation ( N ) ;