# A enumeration method in C++(without loop or recursion..)

• All powers of 4 in bit integer is very few. We can just simply enumerate it...
Although it's not so clever as the 0b1010101010101010101010101010101 method， but it's easy to understand...

``````bool isPowerOfFour(int num) {
if(
num==(1<<0)||
num==(1<<2)||
num==(1<<4)||
num==(1<<6)||
num==(1<<8)||
num==(1<<10)||
num==(1<<12)||
num==(1<<14)||
num==(1<<16)||
num==(1<<18)||
num==(1<<20)||
num==(1<<22)||
num==(1<<24)||
num==(1<<26)||
num==(1<<28)||
num==(1<<30)
){
return true;
}
return false;
}
``````

``````    class Solution {
public:
bool isPowerOfFour(int num) {
vector<int> enums(0);
int temp=1;
int i=0;
while(1){
enums.push_back(temp);
i+=2;
if(temp<1<<i)
temp=1<<i;
else
break;
}
for(int j=0;j<enums.size();++j){
if(num==enums[j])
return true;
}
return false;
}
};``````

• It is a really ugly solution in my opinion and not future proof. Hypothetically speaking, what if in the future architecture changes and we start supporting 16 byte types? Then you would have to find why it fails, which in a big system might not be that easy then do the monkey job and write the rest of? This is not easily maintainable.

• I have to admit that you are right at some point. However, if you want a maintainable code, you could probably write it in loop. I don't see any "maintainble" in the 0b1010101010101010101010101010101 method. Everyone knows how to write your "maintainble" code, but if it has special requirement, things would be a little different. It would be great if you can suggest an "maintainble" without any loop or recursion instead of siiting like this and saying "this is bad ","that is ugly" just like a baby. Anyway, have you ever heard of "LP64", "ILP64", "LLP64"? Have a nice day.

• There is no need to get personal and start calling names. Obviously I have heard of LP64, but this is why I said 16 byte types (whhich would be 128bit architecture). Isn't the point of this site to give code review?

If you want to stick with O(1) time complexity then I would rather use bitwise masks.
return num > 0 && !(num & (num - 1)) && (num & 0x55555555);
Why? If there is a changeit is much easier to expand 0x55555555 by adding a few more fives than adding 30 additional lines.
Again, I did not mean to insult you, if I did, I apologize.
However I do agree that if we are going for readability a loop based version is better, but that is not O(1).

• Sorry, I still agree to disagree with your point that 0x55555555 method is more "maintainable". If the architecture changes, and the coder to fix this is not the same coder who wrote it. It does not seems that the time to understand the masks method is less than the time to copy and paste " 30 additional lines". By the way, a loop based version i mean the code i updated, I think it's still O(1)then, although it's a little slower...Anyway, Thank you for your time on this.
(Although I think the point of this site is to share different ways or thinking processes to solve the problem. Although some methods are delicate, some are clumsy. I believe that the sharing itself is a good thing. I didn't see any point to call someone's thinking process is ugly or stupid.)

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.