Multiples of 4 ( An Interesting Method ) | An interesting XOR based method to check if a number is multiple of 4. ; Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Driver method ; Printing multiples of 4 using above method
class Test { static boolean isMultipleOf4 ( int n ) { if ( n == 1 ) return false ; int XOR = 0 ; for ( int i = 1 ; i <= n ; i ++ ) XOR = XOR ^ i ; return ( XOR == n ) ; } public static void main ( String [ ] args ) { for ( int n = 0 ; n <= 42 ; n ++ ) System . out . print ( isMultipleOf4 ( n ) ? n : " ▁ " ) ; } }