Count even and odd Bitwise XORs of consecutive numbers in a range [ L , R ] starting from L | Java program for the above approach ; Print count of even and odd numbers of XOR value from L to R ; Store the number of elements between L and R ; Count of even XOR values ; If L is odd and range % 4 = 3 ; Increment even by 1 ; If L is even and range % 4 != 0 ; Increment even by 1 ; Print the answer ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void countEvenOdd ( int L , int R ) { int range = R - L + 1 ; int even = ( range / 4 ) * 2 ; if ( ( L & 1 ) != 0 && ( range % 4 == 3 ) ) { even ++ ; } else if ( ( L & 1 ) == 0 && ( range % 4 != 0 ) ) { even ++ ; } System . out . print ( " Even ▁ = ▁ " + even + " , ▁ Odd ▁ = ▁ " + ( range - even ) ) ; } public static void main ( String [ ] args ) { int L = 2 , R = 7 ; countEvenOdd ( L , R ) ; } }