Bitwise Operations on Digits of a Number | Python 3 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
digit = [ 0 ] * ( 100000 ) NEW_LINE def findDigits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT digit [ count ] = n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def OR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans | digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def AND_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans & digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def XOR_of_Digits ( n , count ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( count ) : NEW_LINE INDENT ans = ans ^ digit [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def bitwise_operation ( N ) : NEW_LINE INDENT countOfDigit = findDigits ( N ) NEW_LINE print ( " XOR ▁ = ▁ " , XOR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " OR ▁ = ▁ " , OR_of_Digits ( N , countOfDigit ) ) NEW_LINE print ( " AND ▁ = ▁ " , AND_of_Digits ( N , countOfDigit ) ) NEW_LINE DEDENT N = 123456 ; NEW_LINE bitwise_operation ( N ) NEW_LINE