Looking at links such as this and this, I understand that unsigned int
in C++
should be of 16 bits. As such, the maximum value that it can store should be 32767
.
a. Why can we store INT_MAX in an int variable, such as:
int res=INT_MAX;
b. How is the code like below which calculates the power of 2
valid (runs without any error/warning):
class Solution {
public:
bool isPowerOfTwo(int n) {
return n>0 && (!(n&n-1));
}
};
because the constraints say: -2^31 <= n <= 2^31 - 1
, shouldn't we be using long
?