# Numbers whose bitwise OR and sum with N are equal

Given a non-negative integer N, the task is to find count of non-negative integers

less than or equal to N whose bitwise OR and sum with N are equal.

**Examples :**

Input : N = 3 Output : 1 0 is the only number in [0, 3] that satisfies given property. (0 + 3) = (0 | 3) Input : 10 Output : 4 (0 + 10) = (0 | 10) (Both are 10) (1 + 10) = (1 | 10) (Both are 11) (4 + 10) = (4 | 10) (Both are 14) (5 + 10) = (5 | 10) (Both are 15)

A **simple solution** is to traverse all numbers from 0 to N and do bitwise OR and SUM with N, if both are equal increment counter.

Time complexity = O(N).

An **efficient solution **is to follow following steps.

1. Find count of zero bit in N.

2. Return pow(2,count).

The idea is based on the fact that bitwise OR and sum of a number x with N are equal, if and only if

bitwise AND of x with N will is 0

Let, N=10 =1010_{2}. Bitwise AND of a number with N will be 0, if number contains zero bit with all respective set bit(s) of N and either zero bit or set bit with all respective zero bit(s) of N (because, 0&0=1&0=0).e.g.bit : 1 0 1 0 position: 4 3 2 1 Bitwise AND of any number with N will be 0, if the number has following bit pattern 1^{st}position can be either 0 or 1 (2 ways) 2_{nd}position can be 1 (1 way) 3_{rd}position can be either 0 or 1 (2 ways) 4^{th}position can be 1 (1 way) Total count = 2*1*2*1 = 2^{2}= 4.

## C++

`// C++ program to count numbers whose bitwise ` `// OR and sum with N are equal ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find total 0 bit in a number ` `unsigned ` `int` `CountZeroBit(` `int` `n) ` `{ ` ` ` `unsigned ` `int` `count = 0; ` ` ` `while` `(n) ` ` ` `{ ` ` ` `if` `(!(n & 1)) ` ` ` `count++; ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Function to find Count of non-negative numbers ` `// less than or equal to N, whose bitwise OR and ` `// SUM with N are equal. ` `int` `CountORandSumEqual(` `int` `N ) ` `{ ` ` ` `// count number of zero bit in N ` ` ` `int` `count = CountZeroBit(N); ` ` ` ` ` `// power of 2 to count ` ` ` `return` `(1 << count); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 10; ` ` ` `cout << CountORandSumEqual(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count numbers whose bitwise ` `// OR and sum with N are equal ` `class` `GFG { ` ` ` ` ` `// Function to find total 0 bit in a number ` ` ` `static` `int` `CountZeroBit(` `int` `n) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `while` `(n > ` `0` `) ` ` ` `{ ` ` ` `if` `((n & ` `1` `) != ` `0` `) ` ` ` `count++; ` ` ` ` ` `n >>= ` `1` `; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function to find Count of non-negative ` ` ` `// numbers less than or equal to N, whose ` ` ` `// bitwise OR and SUM with N are equal. ` ` ` `static` `int` `CountORandSumEqual(` `int` `N ) ` ` ` `{ ` ` ` ` ` `// count number of zero bit in N ` ` ` `int` `count = CountZeroBit(N); ` ` ` ` ` `// power of 2 to count ` ` ` `return` `(` `1` `<< count); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` ` ` `int` `N = ` `10` `; ` ` ` ` ` `System.out.print(CountORandSumEqual(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count numbers whose ` `# bitwise OR and sum with N are equal ` ` ` `# Function to find total 0 bit in a number ` `def` `CountZeroBit(n): ` ` ` ` ` `count ` `=` `0` ` ` `while` `(n): ` ` ` ` ` `if` `(` `not` `(n & ` `1` `)): ` ` ` `count ` `+` `=` `1` ` ` `n >>` `=` `1` ` ` ` ` `return` `count ` ` ` `# Function to find Count of non-negative ` `# numbers less than or equal to N, whose ` `# bitwise OR and SUM with N are equal. ` `def` `CountORandSumEqual(N): ` ` ` ` ` `# count number of zero bit in N ` ` ` `count ` `=` `CountZeroBit(N) ` ` ` ` ` `# power of 2 to count ` ` ` `return` `(` `1` `<< count) ` ` ` `# Driver code ` `N ` `=` `10` `print` `(CountORandSumEqual(N)) ` ` ` `# This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count numbers whose ` `// bitwise OR and sum with N are equal ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find total ` ` ` `// 0 bit in a number ` ` ` `static` `int` `CountZeroBit(` `int` `n) ` ` ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n>0) ` ` ` `{ ` ` ` `if` `(n%2!=0) ` ` ` `count++; ` ` ` `n >>= 1; ` ` ` `} ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Function to find Count of non-negative ` ` ` `// numbers less than or equal to N, whose ` ` ` `// bitwise OR and SUM with N are equal. ` ` ` `static` `int` `CountORandSumEqual(` `int` `N ) ` ` ` `{ ` ` ` ` ` `// count number of zero bit in N ` ` ` `int` `count = CountZeroBit(N); ` ` ` ` ` `// power of 2 to count ` ` ` `return` `(1 << count); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 10; ` ` ` `Console.Write(CountORandSumEqual(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ` `// numbers whose bitwise ` `// OR and sum with N are equal ` ` ` `// Function to find total ` `// 0 bit in a number ` ` ` `function` `CountZeroBit(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `if` `(!(` `$n` `& 1)) ` ` ` `$count` `++; ` ` ` `$n` `>>= 1; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Function to find Count of ` `// non-negative numbers less ` `// than or equal to N, whose ` `// bitwise OR and SUM with N ` `// are equal. ` ` ` `function` `CountORandSumEqual(` `$N` `) ` `{ ` ` ` `// count number of ` ` ` `// zero bit in N ` ` ` `$count` `= CountZeroBit(` `$N` `); ` ` ` ` ` `// power of 2 to count ` ` ` `return` `(1 << ` `$count` `); ` `} ` ` ` `// Driver code ` `$N` `= 10; ` `echo` `CountORandSumEqual(` `$N` `); ` ` ` `// This code is contributed by Ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

**Total time complexity of above solution will be O(log _{2}(N)).**

This article is contributed by **Viranjan Kumar Akela**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find N distinct numbers whose bitwise Or is equal to K
- Largest set with bitwise OR equal to n
- Maximum subset with bitwise OR equal to k
- Minimum Bitwise OR operations to make any two array elements equal
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Check whether bitwise OR of N numbers is Even or Odd
- Numbers that are bitwise AND of at least one non-empty sub-array
- Russian Peasant (Multiply two numbers using bitwise operators)
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose XOR with N is equal to OR with N
- Count numbers whose difference with N is equal to XOR with N
- Check if two numbers are equal without using comparison operators
- XOR of two numbers after making length of their binary representations equal