TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

BitFlips

Problem statement, definition, constraints, and public examples.

Problem Statement

Your company builds 20-bit binary counters for industrial applications. You want to test how many bits get changed while incrementing through a particular range. For example, lets say you were incrementing from 1 to 6. The sequence of numbers in binary would be (only 4 low order bits shown for clarity):
0001->0010->0011->0100->0101->0110
From 1 to 2 two bits were changed. From 2 to 3 one bit was changed. From 3 to 4 three bits were changed. Adding up the changes at each increment from 1 to 6, 9 total bits changed. Given low, the number to begin from, and high the number to end at, return the total number of bits changed during the incrementing process.

Definition

Class:
BitFlips
Method:
howMany
Parameters:
int, int
Returns:
int
Method signature:
int howMany(int low, int high)
(be sure your method is public)

Constraints

  • low will be between 0 and 99999 inclusive
  • high will be between 0 and 100000 inclusive
  • low will be less than high

Examples

  1. 1
    6
    Returns: 9
    Example from above
  2. 1
    100000
    Returns: 199993
    As big as it gets
  3. 50
    51
    Returns: 1
  4. 65535
    65536
    Returns: 17
  5. 99
    9999
    Returns: 19796
Back to all problems