Skip to content

Commit 0a19d1a

Browse files
authored
Merge branch 'master' into check_amicable_pair
2 parents 348ece2 + 72cd2d0 commit 0a19d1a

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

.github/workflows/directory_writer.yml

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
name: Directory writer
22
on:
3-
push:
4-
branches:
5-
- main
63
schedule:
74
# ┌───────────── minute (0 - 59)
85
# │ ┌───────────── hour (0 - 23)

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [Count Of Trailing Ciphers In Factorial N](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/count_of_trailing_ciphers_in_factorial_n.cpp)
2121
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
2222
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
23+
* [next higher number with same number of set bits](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp)
2324
* [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp)
2425
* [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp)
2526
* [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)