- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an integer number let’s say, num and the task is to count the smaller numbers less than num whose XOR with num will result in a value greater than the XOR value..

**The truth table for XOR operation is given below**

A | B | A XOR B |

0 | 0 | 0 |

1 | 0 | 1 |

0 | 1 | 1 |

1 | 1 | 0 |

**Input** − int num = 11

**Output** − Count of smaller numbers whose XOR with n produces greater value are − 4

**Explanation** −

We are given with the num as 11 which means we need to find XOR of num with the numbers less than num. So the numbers are 1 XOR 11 < 11(FALSE), 2 XOR 11 < 11(FALSE), 3 XOR 11 < 11(FALSE), 4 XOR 11 > 11(TRUE), 5 XOR 11 > 11(TRUE), 6 XOR 11 > 11(TRUE), 7 XOR 11> 11(TRUE), 8 XOR 11< 11(FALSE), 9 XOR 11 < 11(FALSE), 10 XOR 11 < 11(FALSE).

**Input** − int num = 12

**Output** − Count of smaller numbers whose XOR with n produces greater value are − 3

**Explanation** −

We are given with the num as 12 which means we need to find XOR of num with the numbers less than num. So the numbers are 1 XOR 12 > 12(TRUE), 2 XOR 12 > 12(TRUE), 3 XOR 12 > 12(TRUE), 4 XOR 12 < 12(FALSE), 5 XOR 12 < 12(FALSE), 6 XOR 12 < 12(FALSE), 7 XOR 12< 12(FALSE), 8 XOR 12< 12(FALSE), 9 XOR 12 < 12(FALSE), 10 XOR 12 < 12(FALSE), 11 XOR 12 < 12(FALSE).

Input an integer element and store it in a variable named num.

Pass the value of num to the function for further processing

Create a temporary variable count to store the result.

Start loop WHILE till num > 0

Inside the loop, check IF num & 1 == TRUE then set the count as count + pow(2, temp)

Increment the value of temp by 1

And set num to num >> = 1

Return the count

Print the result

#include using namespace std; //Count smaller numbers whose XOR with n produces greater value int XOR_greater(int n){ int temp = 0; int count = 0; while (n > 0){ if ((n&1) == 0){ count += pow(2, temp); } temp++; n >>= 1; } return count; } int main(){ int n = 20; cout<<"Count of smaller numbers whose XOR with n produces greater value are: "<<XOR_greater(n) << endl; return 0; }

If we run the above code it will generate the following output −

Count of smaller numbers whose XOR with n produces greater value are: 11

- Related Questions & Answers
- Count smaller values whose XOR with x is greater than x in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count of Binary Digit numbers smaller than N in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Count natural numbers whose all permutation are greater than that number in C++
- Find a value whose XOR with given number is maximum in C++
- Count of Smaller Numbers After Self in C++
- Count numbers < = N whose difference with the count of primes upto them is > = K in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
- Count pairs with Odd XOR in C++
- Count of subarrays whose maximum element is greater than k in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Count number of substrings with numeric value greater than X in C++

Advertisements