From e203bfee4e5be699c1d8b28240d0227db696b0d0 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 23 Jun 2023 12:04:16 -0600 Subject: [PATCH 1/3] fix: use correct branch name --- .github/workflows/directory_writer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/directory_writer.yml b/.github/workflows/directory_writer.yml index 06cf942e0..47a62edec 100644 --- a/.github/workflows/directory_writer.yml +++ b/.github/workflows/directory_writer.yml @@ -2,7 +2,7 @@ name: Directory writer on: push: branches: - - main + - master schedule: # ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) From d1ec37c67be44437a57002d7049d1ebd090e9eda Mon Sep 17 00:00:00 2001 From: kunal nayak <87607403+Kunal766@users.noreply.github.com> Date: Sat, 24 Jun 2023 01:15:11 +0530 Subject: [PATCH 2/3] feat: add Next Higher Number with same set bits (#2484) * Next higher number with same set bits implimented * Added to DIRECTORY.md * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp new line aded Co-authored-by: David Leal * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp * added Co-authored-by: David Leal * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp recomendation added Co-authored-by: David Leal * Update next_higher_number_with_same_number_of_set_bits.cpp int to int64_t * Update bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --------- Co-authored-by: David Leal Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> --- DIRECTORY.md | 1 + ...er_number_with_same_number_of_set_bits.cpp | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 91fd3ef28..05ad073af 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,7 @@ * [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) * [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp) * [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp) + * [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) * [Power Of 2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/power_of_2.cpp) * [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp) * [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp) diff --git a/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp b/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp new file mode 100644 index 000000000..63d6a1f03 --- /dev/null +++ b/bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp @@ -0,0 +1,103 @@ +/** + * @file + * @brief [Next higher number with same number of set bits] + * (https://www.geeksforgeeks.org/next-higher-number-with-same-number-of-set-bits/) + * implementation + * + * @details + * Given a number x, find next number with same number of 1 bits in it’s binary representation. + * For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine). + * It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012). + * + * A binary number consists of two digits. They are 0 & 1. Digit 1 is known as + * set bit in computer terms. + * @author [Kunal Nayak](https://github.com/Kunal766) + */ + +#include /// for assert +#include /// for IO operations + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { + +/** + * @brief The main function implements checking the next number + * @param x the number that will be calculated + * @returns a number + */ +uint64_t next_higher_number(uint64_t x) +{ + + uint64_t rightOne; + uint64_t nextHigherOneBit; + uint64_t rightOnesPattern; + + uint64_t next = 0; + + if(x) + { + + // right most set bit + rightOne = x & -(signed)x; + + // reset the pattern and set next higher bit + // left part of x will be here + nextHigherOneBit = x + rightOne; + + // nextHigherOneBit is now part [D] of the above explanation. + + // isolate the pattern + rightOnesPattern = x ^ nextHigherOneBit; + + // right adjust pattern + rightOnesPattern = (rightOnesPattern)/rightOne; + + // correction factor + rightOnesPattern >>= 2; + + // rightOnesPattern is now part [A] of the above explanation. + + // integrate new pattern (Add [D] and [A]) + next = nextHigherOneBit | rightOnesPattern; + } + + return next; +} + +} // namespace bit_manipulation + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // x = 4 return 8 + assert(bit_manipulation::next_higher_number(4) == 8); + // x = 6 return 9 + assert(bit_manipulation::next_higher_number(6) == 9); + // x = 13 return 14 + assert(bit_manipulation::next_higher_number(13) == 14); + // x = 64 return 128 + assert(bit_manipulation::next_higher_number(64) == 128); + // x = 15 return 23 + assert(bit_manipulation::next_higher_number(15) == 23); + // x= 32 return 64 + assert(bit_manipulation::next_higher_number(32) == 64); + // x = 97 return 98 + assert(bit_manipulation::next_higher_number(97) == 98); + // x = 1024 return 2048 + assert(bit_manipulation::next_higher_number(1024) == 2048); + + std::cout << "All test cases have successfully passed!" << std::endl; +} +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} From 72cd2d0eb97d72e3a667895fcd83212e61260b01 Mon Sep 17 00:00:00 2001 From: David Leal Date: Fri, 23 Jun 2023 13:51:00 -0600 Subject: [PATCH 3/3] fix: directory writer `on` event --- .github/workflows/directory_writer.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/directory_writer.yml b/.github/workflows/directory_writer.yml index 47a62edec..3264c0324 100644 --- a/.github/workflows/directory_writer.yml +++ b/.github/workflows/directory_writer.yml @@ -1,8 +1,5 @@ name: Directory writer on: - push: - branches: - - master schedule: # ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23)