XOR counts of 0s and 1s in binary representation Last Updated : 11 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Given a number, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number. Examples: Input : 5 Output : 3 Binary representation : 101 Count of 0s = 1, Count of 1s = 2 1 XOR 2 = 3. Input : 7 Output : 3 Binary representation : 111 Count of 0s = 0 Count of 1s = 3 0 XOR 3 = 3. Recommended PracticeXOR Count Zero and OneTry It! The idea is simple, we traverse through all bits of a number, count 0s and 1s and finally return XOR of two counts. C++ // C++ program to find XOR of counts 0s and 1s in // binary representation of n. #include<iostream> using namespace std; // Returns XOR of counts 0s and 1s in // binary representation of n. int countXOR(int n) { int count0 = 0, count1 = 0; while (n) { //calculating count of zeros and ones (n % 2 == 0) ? count0++ :count1++; n /= 2; } return (count0 ^ count1); } // Driver Program int main() { int n = 31; cout << countXOR (n); return 0; } Java // Java program to find XOR of counts 0s // and 1s in binary representation of n. class GFG { // Returns XOR of counts 0s and 1s // in binary representation of n. static int countXOR(int n) { int count0 = 0, count1 = 0; while (n != 0) { //calculating count of zeros and ones if(n % 2 == 0) count0++ ; else count1++; n /= 2; } return (count0 ^ count1); } // Driver Program public static void main(String[] args) { int n = 31; System.out.println(countXOR (n)); } } // This code is contributed by prerna saini Python3 # Python3 program to find XOR of counts 0s # and 1s in binary representation of n. # Returns XOR of counts 0s and 1s # in binary representation of n. def countXOR(n): count0, count1 = 0, 0 while (n != 0): # calculating count of zeros and ones if(n % 2 == 0): count0 += 1 else: count1 += 1 n //= 2 return (count0 ^ count1) # Driver Code n = 31 print(countXOR(n)) # This code is contributed by Anant Agarwal. C# // C# program to find XOR of counts 0s // and 1s in binary representation of n. using System; class GFG { // Returns XOR of counts 0s and 1s // in binary representation of n. static int countXOR(int n) { int count0 = 0, count1 = 0; while (n != 0) { // calculating count of zeros // and ones if(n % 2 == 0) count0++ ; else count1++; n /= 2; } return (count0 ^ count1); } // Driver Program public static void Main() { int n = 31; Console.WriteLine(countXOR (n)); } } // This code is contributed by Anant Agarwal. PHP <?PHP // PHP program to find XOR of // counts 0s and 1s in binary // representation of n. // Returns XOR of counts 0s and 1s // in binary representation of n. function countXOR($n) { $count0 = 0; $count1 = 0; while ($n) { // calculating count of // zeros and ones ($n % 2 == 0) ? $count0++ :$count1++; $n = intval($n / 2); } return ($count0 ^ $count1); } // Driver Code $n = 31; echo countXOR ($n); // This code is contributed // by ChitraNayal ?> JavaScript <script> // Javascript program to find XOR of counts 0s // and 1s in binary representation of n. // Returns XOR of counts 0s and 1s // in binary representation of n. function countXOR(n) { let count0 = 0, count1 = 0; while (n != 0) { //calculating count of zeros and ones if(n % 2 == 0) count0++ ; else count1++; n = Math.floor(n/2); } return (count0 ^ count1); } // Driver Program let n = 31; document.write(countXOR (n)); // This code is contributed by avanitrachhadiya2155 </script> Output: 5 Time Complexity: O(log(N))Auxiliary Space: O(1) One observation is, for a number of the form 2^x - 1, the output is always x. We can directly produce answer for this case by first checking n+1 is a power of two or not. Comment More infoAdvertise with us Next Article XOR counts of 0s and 1s in binary representation K kartik Improve Article Tags : Bit Magic DSA Bitwise-XOR Practice Tags : Bit Magic Similar Reads Binary representation of a given number Given an integer n, the task is to print the binary representation of the number. Note: The given number will be maximum of 32 bits, so append 0's to the left if the result string is smaller than 30 length.Examples: Input: n = 2Output: 00000000000000000000000000000010Input: n = 0Output: 000000000000 6 min read Count of binary string of length N with X 0s and Y 1s Given positive integers N, X and Y. The task is to find the count of unique binary strings of length N having X 0s and Y 1s. Examples: Input: N=5, X=3, Y=2Output: 10Explanation: There are 10 binary strings of length 5 with 3 0s and 2 1s, such as: 00011, 00101, 01001, 10001, 00110, 01010, 10010, 0110 4 min read Count of numbers in range [L, R] with LSB as 0 in their Binary representation Given two integers L and R. The task is to find the count of all numbers in the range [L, R] whose Least Significant Bit in binary representation is 0. Examples: Input: L = 10, R = 20 Output: 6 Input: L = 7, R = 11 Output: 2 Naive approach: The simplest approach is to solve this problem is to check 5 min read Check if an encoding represents a unique binary string Given an encoding of a binary string of length k, the task is to find if the given encoding uniquely identifies a binary string or not. The encoding has counts of contiguous 1s (separated by 0s). For example, encoding of 11111 is {5}, encoding of 01101010 is {2, 1, 1} and encoding of 111011 is {3, 2 6 min read Count trailing zeroes present in binary representation of a given number using XOR Given an integer N, the task is to find the number of trailing zeroes in the binary representation of the given number. Examples: Input: N = 12Output: 2Explanation:The binary representation of the number 13 is "1100".Therefore, there are two trailing zeros in the 12. Input: N = -56Output: 3Explanati 4 min read Like