Skip to content

Commit cb3c8f5

Browse files
committed
Day 5: Program to check if a number is power of 4
1 parent 39d77be commit cb3c8f5

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 140 |
10-
| Current Streak | 4 days |
9+
| Total Problems | 141 |
10+
| Current Streak | 5 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -72,6 +72,7 @@ Include contains single header implementation of data structures and some algori
7272
|Louise and Richard play a game. They have a counter set to N. Louise gets the first turn and the turns alternate thereafter. In the game, they perform the following operations: <ul><li>If N is not a power of 2, reduce the counter by the largest power of 2 less than N.</li></ul><ul><li>If N is a power of 2, reduce the counter by half of N.</li></ul> The resultant value is the new N which is again used for subsequent operations.The game ends when the counter reduces to 1, i.e., N == 1, and the last person to make a valid move wins. <ul><li> Given N, your task is to find the winner of the game. If they set counter to 1, Richard wins, because its Louise' turn and she cannot make a move.</li></ul><ul><li>Input Format : -The first line contains an integer T, the number of testcases. T lines follow. Each line contains N, the initial number set in the counter.</ul></li> |[counter_game.cpp](bit_manipulation/counter_game.cpp)|
7373
|Determine if two integers are of opposite signs.|[check_opposite_signs.cpp](bit_manipulation/check_opposite_signs.cpp)|
7474
|Swap two bits at position p and q of a given integer.| [swapBits.cpp](bit_manipulation/swapBits.cpp)|
75+
|Check if a number is power of 4. | [check_if_power_of_4.cpp](bit_manipulation/check_if_power_of_4.cpp)|
7576

7677

7778
### Cracking the coding interview problems
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Check if a given number is power of 4 or not.
3+
*
4+
* Approach: If a value is power of 4, it has to be power of 2 and
5+
* it will have set bits at even position (as the number is power of 2, it will have
6+
* only one set bit.) For example:
7+
* 4 (10)
8+
* 16 (10000)
9+
* 64 (1000000)
10+
* If a number is power of 2 then it would have only one set bit and that set
11+
* bit would be reset by the expression (n & (n-1)), thus if (n & (n-1)) == 0,
12+
* means n is power of 2.
13+
* Now, since we have one set bit and if it is at even position it would be
14+
* power of 4, to check if set bit is at even position, we should AND it with
15+
* expression 10101010101010101010101010101010 i.e. 0xAAAAAAAA. Notice we have
16+
* set bits at all odd positions (it is 0 indexed), thus if expression
17+
* (n & 0xAAAAAAAA) == 0, then we have that set bit at even position.
18+
*/
19+
20+
#include <iostream>
21+
22+
bool isPowerOf4 (unsigned int n)
23+
{
24+
// The number should be power of 2, and should have set bit at even position
25+
//
26+
return (n && !(n & (n-1)) && !(n & 0xAAAAAAAA));
27+
}
28+
29+
int main()
30+
{
31+
unsigned int n;
32+
std::cout << "Enter a number:";
33+
std::cin >> n;
34+
if (isPowerOf4(n))
35+
{
36+
std::cout << n << " is power of 4.\n";
37+
}
38+
else
39+
{
40+
std::cout << n << " is not a power of 4.\n";
41+
}
42+
}

0 commit comments

Comments
 (0)