Repository: TheAlgorithms/C-Plus-Plus
File: bit_manipulation/next_higher_number_with_same_number_of_set_bits.cpp (C++)

Code before:
 *
 * @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 <cassert>   /// for assert
#include <iostream>  /// for IO operations

/**
 * @namespace bit_manipulation
 * @brief Bit manipulation algorithms
 */
namespace bit_manipulation {
/**
 * @brief The main function implements check next numbet
 * @param x is the number who's next number will be calculated
 * @returns a number
 */

int next_higher_number(int x)
{
 
  int rightOne;
  int nextHigherOneBit;
  int rightOnesPattern;
 
  int 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;

Review comment (suggestion):
Consider using `uint64_t` for non-negative values (or their appropriate size: `uint32_t`, `uint16_t`, `uint8_t`) or `int64_t` for negative values. Check other parts of the code ([reference](https://en.cppreference.com/w/cpp/types/integer)). **Let us know if you need any help here or on our [Discord server](https://the-algorithms.com/discord).** 🙂


Code after:
 *
 * @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 <cassert>   /// for assert
#include <iostream>  /// 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;