-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
power_of_two.cpp
106 lines (91 loc) · 2.77 KB
/
power_of_two.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @file
* @brief Implementation to check whether a number is a power of 2 or not.
*
* @details
* This algorithm uses bit manipulation to check if a number is a power of 2 or
* not.
*
* ### Algorithm
* Let the input number be n, then the bitwise and between n and n-1 will let us
* know whether the number is power of 2 or not
*
* For Example,
* If N= 32 then N-1 is 31, if we perform bitwise and of these two numbers then
* the result will be zero, which indicates that it is the power of 2
* If N=23 then N-1 is 22, if we perform bitwise and of these two numbers then
* the result will not be zero , which indicates that it is not the power of 2
* \note This implementation is better than naive recursive or iterative
* approach.
*
* @author [Neha Hasija](https://github.com/neha-hasija17)
* @author [Rijul.S](https://github.com/Rijul24)
*/
#include <iostream> /// for IO operations
#include <cassert> /// for assert
/**
* @namespace math
* @brief Mathematical algorithms
*/
namespace math {
/**
* @brief This function finds whether a number is power of 2 or not
* @param n value for which we want to check
* prints the result, as "Yes, the number n is a power of 2" or
* "No, the number is not a power of 2" without quotes
* @returns 1 if `n` IS the power of 2
* @returns 0 if n is NOT a power of 2
*/
int power_of_two(int n) {
/// result stores the
/// bitwise and of n and n-1
int result = n & (n - 1);
if (result == 0) {
return 1;
}
return 0;
}
} // namespace math
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
std::cout << "First case testing... \n"; // for n = 32 return 1
assert(math::power_of_two(32) == 1);
std::cout << "\nPassed!\n";
std::cout << "Second case testing... \n"; // for n = 5 return 0
assert(math::power_of_two(5) == 0);
std::cout << "\nPassed!\n";
std::cout << "Third case testing... \n"; // for n = 232 return 0
assert(math::power_of_two(232) == 0);
std::cout << "\nPassed!\n";
std::cout << "\nAll test cases have successfully passed!\n";
}
/**
* @brief Take user input in the test cases (optional; currently commented)
* @returns void
*/
void user_input_test() {
int n = 0; // input from user
std::cout << "Enter a number " << std::endl;
std::cin >> n;
/// function call with @param n
int result = math::power_of_two(n);
if (result == 1) {
std::cout << "Yes, the number " << n << " is a power of 2\n";
}
else {
std::cout << "No, the number " << n << " is not a power of 2\n";
}
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
// uncomment the line below to take user inputs
//user_input_test();
return 0;
}