Bitwise XOR of all odd numbers from a given range | Java program for the above approach ; 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 ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call
import java . util . * ; class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }