Bitwise XOR of all odd numbers from a given range | Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Prthe answer ; If number is odd ; Prthe answer ; Driver Code ; Function Call
def findXOR ( n ) : NEW_LINE INDENT if ( n % 4 == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT elif ( n % 4 == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( n % 4 == 2 ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT elif ( n % 4 == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def findOddXOR ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n // 2 ) ) ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) // 2 ) ) ) ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 11 ; NEW_LINE findOddXOR ( N ) ; NEW_LINE DEDENT