|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief [Next higher number with same number of set bits] |
| 4 | + * (https://www.geeksforgeeks.org/next-higher-number-with-same-number-of-set-bits/) |
| 5 | + * implementation |
| 6 | + * |
| 7 | + * @details |
| 8 | + * Given a number x, find next number with same number of 1 bits in it’s binary representation. |
| 9 | + * For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine). |
| 10 | + * It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012). |
| 11 | + * |
| 12 | + * A binary number consists of two digits. They are 0 & 1. Digit 1 is known as |
| 13 | + * set bit in computer terms. |
| 14 | + * @author [Kunal Nayak](https://github.com/Kunal766) |
| 15 | + */ |
| 16 | + |
| 17 | +#include <cassert> /// for assert |
| 18 | +#include <iostream> /// for IO operations |
| 19 | + |
| 20 | +/** |
| 21 | + * @namespace bit_manipulation |
| 22 | + * @brief Bit manipulation algorithms |
| 23 | + */ |
| 24 | +namespace bit_manipulation { |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief The main function implements checking the next number |
| 28 | + * @param x the number that will be calculated |
| 29 | + * @returns a number |
| 30 | + */ |
| 31 | +uint64_t next_higher_number(uint64_t x) |
| 32 | +{ |
| 33 | + |
| 34 | + uint64_t rightOne; |
| 35 | + uint64_t nextHigherOneBit; |
| 36 | + uint64_t rightOnesPattern; |
| 37 | + |
| 38 | + uint64_t next = 0; |
| 39 | + |
| 40 | + if(x) |
| 41 | + { |
| 42 | + |
| 43 | + // right most set bit |
| 44 | + rightOne = x & -(signed)x; |
| 45 | + |
| 46 | + // reset the pattern and set next higher bit |
| 47 | + // left part of x will be here |
| 48 | + nextHigherOneBit = x + rightOne; |
| 49 | + |
| 50 | + // nextHigherOneBit is now part [D] of the above explanation. |
| 51 | + |
| 52 | + // isolate the pattern |
| 53 | + rightOnesPattern = x ^ nextHigherOneBit; |
| 54 | + |
| 55 | + // right adjust pattern |
| 56 | + rightOnesPattern = (rightOnesPattern)/rightOne; |
| 57 | + |
| 58 | + // correction factor |
| 59 | + rightOnesPattern >>= 2; |
| 60 | + |
| 61 | + // rightOnesPattern is now part [A] of the above explanation. |
| 62 | + |
| 63 | + // integrate new pattern (Add [D] and [A]) |
| 64 | + next = nextHigherOneBit | rightOnesPattern; |
| 65 | + } |
| 66 | + |
| 67 | + return next; |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace bit_manipulation |
| 71 | + |
| 72 | +/** |
| 73 | + * @brief Self-test implementations |
| 74 | + * @returns void |
| 75 | + */ |
| 76 | +static void test() { |
| 77 | + // x = 4 return 8 |
| 78 | + assert(bit_manipulation::next_higher_number(4) == 8); |
| 79 | + // x = 6 return 9 |
| 80 | + assert(bit_manipulation::next_higher_number(6) == 9); |
| 81 | + // x = 13 return 14 |
| 82 | + assert(bit_manipulation::next_higher_number(13) == 14); |
| 83 | + // x = 64 return 128 |
| 84 | + assert(bit_manipulation::next_higher_number(64) == 128); |
| 85 | + // x = 15 return 23 |
| 86 | + assert(bit_manipulation::next_higher_number(15) == 23); |
| 87 | + // x= 32 return 64 |
| 88 | + assert(bit_manipulation::next_higher_number(32) == 64); |
| 89 | + // x = 97 return 98 |
| 90 | + assert(bit_manipulation::next_higher_number(97) == 98); |
| 91 | + // x = 1024 return 2048 |
| 92 | + assert(bit_manipulation::next_higher_number(1024) == 2048); |
| 93 | + |
| 94 | + std::cout << "All test cases have successfully passed!" << std::endl; |
| 95 | +} |
| 96 | +/** |
| 97 | + * @brief Main function |
| 98 | + * @returns 0 on exit |
| 99 | + */ |
| 100 | +int main() { |
| 101 | + test(); // run self-test implementations |
| 102 | + return 0; |
| 103 | +} |
0 commit comments