Count smaller numbers whose XOR with n produces greater value
Last Updated :
21 Jul, 2022
Given a positive integer n, count numbers x such that 0 < x <n and x^n > n where ^ is bitwise XOR operation.
Examples:
Input : n = 12
Output : 3
Numbers are 1, 2 and 3
1^12 > 12, 2^12 > 12 and 3^12 > 12
Input : n = 11
Output : 4
Numbers are 4, 5, 6 and 7
A number may x produce a greater XOR value if x has a set bit at a position where n has a 0 bit. So we traverse bits of n, and one by one consider all 0 bits. For every set bit at position k (Considering k = 0 for rightmost bit, k = 1 for second rightmost bit, ..), we add 2 2k to result. For a bit at k-th position, there are 2k numbers with set bit 1.
Below is the implementation of the above idea.
C++
// C++ program to count numbers whose XOR with n
// produces a value more than n.
#include<bits/stdc++.h>
using namespace std;
int countNumbers(int n)
{
/* If there is a number like m = 11110000,
then it is bigger than 1110xxxx. x can either
0 or 1. So we have pow(2, k) greater numbers
where k is position of rightmost 1 in m. Now
by using XOR bit at each position can be changed.
To change bit at any position, it needs to XOR
it with 1. */
int k = 0; // Position of current bit in n
/* Traverse bits from LSB (least significant bit)
to MSB */
int count = 0; // Initialize result
while (n > 0)
{
// If current bit is 0, then there are
// 2^k numbers with current bit 1 and
// whose XOR with n produces greater value
if ((n&1) == 0)
count += pow(2, k);
// Increase position for next bit
k += 1;
// Reduce n to find next bit
n >>= 1;
}
return count;
}
// Driver code
int main()
{
int n = 11;
cout << countNumbers(n) << endl;
return 0;
}
Java
// Java program to count numbers
// whose XOR with n produces a
// value more than n.
import java.lang.*;
class GFG {
static int countNumbers(int n)
{
// If there is a number like
// m = 11110000, then it is
// bigger than 1110xxxx. x
// can either be 0 or 1. So
// where k is the position of
// rightmost 1 in m. Now by
// using the XOR bit at each
// position can be changed.
// To change the bit at any
// position, it needs to
// XOR it with 1.
int k = 0;
// Position of current bit in n
// Traverse bits from LSB (least
// significant bit) to MSB
int count = 0;
// Initialize result
while (n > 0) {
// If the current bit is 0, then
// there are 2^k numbers with
// current bit 1 and whose XOR
// with n produces greater value
if ((n & 1) == 0)
count += (int)(Math.pow(2, k));
// Increase position for next bit
k += 1;
// Reduce n to find next bit
n >>= 1;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int n = 11;
System.out.println(countNumbers(n));
}
}
// This code is contributed by Smitha.
Python3
# Python program to count numbers whose
# XOR with n produces a value more than n.
def countNumbers(n):
''' If there is a number like m = 11110000,
then it is bigger than 1110xxxx. x can either
0 or 1. So we have pow(2, k) greater numbers
where k is position of rightmost 1 in m. Now
by using XOR bit at each position can be changed.
To change bit at any position, it needs to XOR
it with 1. '''
# Position of current bit in n
k = 0
# Traverse bits from
# LSB to MSB
count = 0 # Initialize result
while (n > 0):
# If current bit is 0, then there are
# 2^k numbers with current bit 1 and
# whose XOR with n produces greater value
if ((n & 1) == 0):
count += pow(2, k)
# Increase position for next bit
k += 1
# Reduce n to find next bit
n >>= 1
return count
# Driver code
n = 11
print(countNumbers(n))
# This code is contributed by Anant Agarwal.
C#
// C# program to count numbers
// whose XOR with n produces a
// value more than n.
using System;
class GFG {
static int countNumbers(int n)
{
// If there is a number like
// m = 11110000, then it is
// bigger than 1110xxxx. x
// can either be 0 or 1. So
// where k is the position of
// rightmost 1 in m. Now by
// using the XOR bit at each
// position can be changed.
// To change the bit at any
// position, it needs to
// XOR it with 1.
int k = 0;
// Position of current bit in n
// Traverse bits from LSB (least
// significant bit) to MSB
int count = 0;
// Initialize result
while (n > 0) {
// If the current bit is 0, then
// there are 2^k numbers with
// current bit 1 and whose XOR
// with n produces greater value
if ((n & 1) == 0)
count += (int)(Math.Pow(2, k));
// Increase position for next bit
k += 1;
// Reduce n to find next bit
n >>= 1;
}
return count;
}
// Driver code
public static void Main()
{
int n = 11;
Console.WriteLine(countNumbers(n));
}
}
// This code is contributed by Anant Agarwal.
PHP
<?php
// PHP program to count
// numbers whose XOR with n
// produces a value more than n.
function countNumbers($n)
{
/* If there is a number
like m = 11110000,
then it is bigger
than 1110xxxx. x
can either 0 or 1.
So we have pow(2, k)
greater numbers where
k is position of
rightmost 1 in m.
Now by using XOR bit
at each position can
be changed. To change
bit at any position,
it needs to XOR it
with 1. */
// Position of current bit in n
$k = 0;
/* Traverse bits from LSB
(least significant bit)
to MSB */
// Initialize result
$count = 0;
while ($n > 0)
{
// If current bit is 0,
// then there are 2^k
// numbers with current
// bit 1 and whose XOR
// with n produces greater
// value
if (($n & 1) == 0)
$count += pow(2, $k);
// Increase position
// for next bit
$k += 1;
// Reduce n to
// find next bit
$n >>= 1;
}
return $count;
}
// Driver code
$n = 11;
echo countNumbers($n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to count numbers whose XOR with n
// produces a value more than n.
function countNumbers(n)
{
// If there is a number like
// m = 11110000, then it is
// bigger than 1110xxxx. x
// can either be 0 or 1. So
// where k is the position of
// rightmost 1 in m. Now by
// using the XOR bit at each
// position can be changed.
// To change the bit at any
// position, it needs to
// XOR it with 1.
let k = 0;
// Position of current bit in n
// Traverse bits from LSB (least
// significant bit) to MSB
let count = 0;
// Initialize result
while (n > 0) {
// If the current bit is 0, then
// there are 2^k numbers with
// current bit 1 and whose XOR
// with n produces greater value
if ((n & 1) == 0)
count += (Math.pow(2, k));
// Increase position for next bit
k += 1;
// Reduce n to find next bit
n >>= 1;
}
return count;
}
// Driver Code
let n = 11;
document.write(countNumbers(n));
</script>
Output:
4
Time complexity: O(logn)
Auxiliary Space: O(1)
Similar Reads
Count smaller values whose XOR with x is greater than x Given a integer 'x', find the number of values of 'a' satisfying the following conditions: a XOR x > x0 < a < x Examples : Input : x = 10 Output : 5 Explanation: For x = 10, following 5 values of 'a' satisfy the conditions: 1 XOR 10 = 11 4 XOR 10 = 14 5 XOR 10 = 15 6 XOR 10 = 12 7 XOR 10 =
7 min read
Count of all possible values of X whose Bitwise XOR with N is greater than N Given an integer N count the number of values of X such that XâN > N, where â denotes bitwise XOR operation Examples: Input: N = 10Output: 5 Explanation: The five possible value satisfying the above condition are:1â10 = 11, 4â10 = 14, 5â10 = 15, 6â10 = 12, 7â10 = 13 Input: N = 8Output: 7Explanati
6 min read
Count numbers whose XOR with N is equal to OR with N Given a number N, the task is to find the count of X such that N XOR X == N OR X, where 0<=X<=N Examples: Input: N = 5 Output: 2 For N = 5, 5 XOR 2 == 5 OR 2 5 XOR 0 == 5 OR 0 Thus, count is 2.Input: N = 7 Output: 1 For N = 7, 7 XOR 0 == 7 OR 0 Thus, count is 1. Recommended: Please solve it on
4 min read
Count numbers whose sum with x is equal to XOR with x Given a integer âxâ, find the number of values of âaâ satisfying the following conditions: 0 <= a <= xa XOR x = a + x Examples : Input : 5 Output : 2 Explanation: For x = 5, following 2 values of 'a' satisfy the conditions: 5 XOR 0 = 5+0 5 XOR 2 = 5+2 Input : 10 Output : 4 Explanation: For x =
9 min read
Count subarrays with sum equal to its XOR value Given an array arr[] containing N elements, the task is to count the number of sub-arrays whose XOR of all the elements is equal to the sum of all the elements in the subarray. Examples: Input: arr[] = {2, 5, 4, 6} Output: 5 Explanation: All the subarrays {{2}, {5}, {4}, {6}} satisfies the above con
10 min read
Count numbers whose difference with N is equal to XOR with N Given a number N. The task is to count all possible values of x such that n\oplus x is equal to (N-x), where \oplus denotes bitwise XOR operation.Examples: Input: N = 3 Output: 4 The all possible values of x are respectively 0, 1, 2, 3. Input: N = 6 Output: 4 The all possible values of x are respect
4 min read