How to find number of integers in a given range has even set bits

NickThomsan

I want to know how many numbers say x, in a given range lets say l and r, l < r, present where number of 1's in binary representation of x is even. Is there any efficient way to find that?

rici

Here's a key fact:

  • If n is a non-negative even number, then exactly half of the non-negative integers less than n have even parity and the other half have odd parity. ("Even parity" means that the number of bits in the binary representation is even.)

Suppose we need to count the number of integers in the range [l, r) with some property P, and we know how to solve this problem for any range where l is 0. ( "[l, r)" is a "half-open range": all integers n where ln<r. That makes the arithmetic easier.) Then we just need to subtract in order to solve the general problem where l≠0: COUNT[l,r) = COUNT[0,r) − COUNT[0,l).

The first fact doesn't quite tell us all we need to know, since it only works for even n. But if n is odd, n-1 is even, and all we need to do is check the parity of n-1 itself, which is the extra number not in the range [0,n-1).

Putting all that together, if we have the range [l, r), we compute the count as follows:

  • COUNT(0,l) is l/2 if l is even, and (l-1)/2+PARITY(l) if l is odd
  • COUNT(0,r) is r/2 if r is even, and (r-1)/2+PARITY(r) if r is odd
  • COUNT(l,r) is COUNT(0,r) − COUNT(0,l)

That last computation requires at most two parity computations, regardless of how large the range is, as well as a couple of divisions and a subtraction.

If this were a mathematics site, I might feel compelled to prove the assertion in the key fact at the beginning, but since this is CS I will content myself with a proof outline. We first note that if i is even, then PARITY(i) and PARITY(i+1) are different (since the binary representations only differ in the last bit). Conversely, if i is odd, then PARITY(i) and PARITY(i-1) are different. Now, take all the integers in [0,n) and divide them into the set of integers with odd parity and the set with even parity, and consider the homomorphism

f(i)⇒i+1 if i is even;
f(i)⇒i−1 if i is odd;

The image of f over one of the two subsets of [0,n) is the other subset, since the parity of f(i) is different from the parity of i. So the two subsets are the same size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find a range of given number?

From Dev

Given a file containing 4.30 billion 32-bit integers, how can we find a number, which has appeared at least twice?

From Dev

how to find number that's divisible by a number in a given range?

From Dev

Given a set of intervals, find the interval which has the maximum number of intersections

From Dev

sum of BITS is given find thar number

From Dev

How to look up range from set of contiguous ranges for given number

From Dev

How can a range of even integers be defined in XSD?

From Dev

How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

From Dev

Given a set of intervals, how to find the maximum number of intersections among them,

From Dev

Given N bits, how many integers can be represented in binary?

From Dev

Find the closest higher number that has a given divisor

From Dev

JQuery - Find element that has a given number of siblings

From Dev

How to store number of elements exceeding the range of integers

From Dev

How to store number of elements exceeding the range of integers

From Dev

Calculating the next higher number which has same number of set bits?

From Dev

Logic to set a bits of given number from first occurance of bits 1 as given example?

From Dev

How to check if a float value is within a certain range and has a given number of decimal digits?

From Dev

Find Pair Of Integers in Array whose Sum is Given Number in PHP

From Dev

count no. of set bits of each number in a range and then sum it up

From Dev

Find out how many zeros a number has at the end (string index out of range error when number is 0)

From Dev

Find number of rectangles from a given set of coordinates

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

How to find the top number between a range given a value from a table using LINQ

From Dev

jQuery: how to generate 3 different random integers given limits/range?

From Dev

How to iterate from a given number range?

From Dev

How to SUM the number of duplicates in a range by given value?

From Dev

How to check if value has even parity of bits or odd?

From Dev

How to check if value has even parity of bits or odd?

From Java

How to find the number that best matches a given number

Related Related

  1. 1

    How to find a range of given number?

  2. 2

    Given a file containing 4.30 billion 32-bit integers, how can we find a number, which has appeared at least twice?

  3. 3

    how to find number that's divisible by a number in a given range?

  4. 4

    Given a set of intervals, find the interval which has the maximum number of intersections

  5. 5

    sum of BITS is given find thar number

  6. 6

    How to look up range from set of contiguous ranges for given number

  7. 7

    How can a range of even integers be defined in XSD?

  8. 8

    How to find all ordered pairs of elements in array of integers whose sum lies in a given range of value

  9. 9

    Given a set of intervals, how to find the maximum number of intersections among them,

  10. 10

    Given N bits, how many integers can be represented in binary?

  11. 11

    Find the closest higher number that has a given divisor

  12. 12

    JQuery - Find element that has a given number of siblings

  13. 13

    How to store number of elements exceeding the range of integers

  14. 14

    How to store number of elements exceeding the range of integers

  15. 15

    Calculating the next higher number which has same number of set bits?

  16. 16

    Logic to set a bits of given number from first occurance of bits 1 as given example?

  17. 17

    How to check if a float value is within a certain range and has a given number of decimal digits?

  18. 18

    Find Pair Of Integers in Array whose Sum is Given Number in PHP

  19. 19

    count no. of set bits of each number in a range and then sum it up

  20. 20

    Find out how many zeros a number has at the end (string index out of range error when number is 0)

  21. 21

    Find number of rectangles from a given set of coordinates

  22. 22

    Find the minimum set of integers whose sum is greater than a given integer

  23. 23

    How to find the top number between a range given a value from a table using LINQ

  24. 24

    jQuery: how to generate 3 different random integers given limits/range?

  25. 25

    How to iterate from a given number range?

  26. 26

    How to SUM the number of duplicates in a range by given value?

  27. 27

    How to check if value has even parity of bits or odd?

  28. 28

    How to check if value has even parity of bits or odd?

  29. 29

    How to find the number that best matches a given number

HotTag

Archive