-2

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?

J. Doe
  • 857
  • 3
  • 7
  • Please also leave a comment clarifying my confusion. Thank you. – J. Doe Jul 11 '22 at 15:29
  • 3
    By looking at the pages you linked, I cannot conclude that c++ `unsigned int` is required to be 16 bits. The standard requires it to have **at least** 16 bits. – Jakob Stark Jul 11 '22 at 15:29
  • 1
    @JakobStark, oh, so both the code snippets above are platform dependent? – J. Doe Jul 11 '22 at 15:30
  • 2
    For most platforms `unsigned int` has 32 bits (Windows/Linux/MacOS/iOS/Android/...). For example on Arduino it is 16 bits. – Marek R Jul 11 '22 at 15:30
  • 1
    Please page down to the __Properties__ table [Fundamental types](https://en.cppreference.com/w/cpp/language/types) `signed int` at least 16. And then check your compiler with `static_assert(sizeof(unsigned int) == 16);` – Richard Critten Jul 11 '22 at 15:31
  • 2
    The size of `int` (and many more fundamental types) is dependent on the platform you're compiling for. If you need a specific width, use `std::int16_t` or equivalent – perivesta Jul 11 '22 at 15:32
  • @perivesta: I can't think of a fundamental type that isn't platform dependent in some way. Can you? – Bathsheba Jul 11 '22 at 15:32
  • @RichardCritten: Indeed, always 1, but I've worked on a system where there were 64 bits in a byte. – Bathsheba Jul 11 '22 at 15:34
  • @RichardCritten, yes, I read that. But I did not know all modern architectures have use 32 bits for unsigned int. Everywhere I just encountered values around 2^31 being stored in `int`s. – J. Doe Jul 11 '22 at 15:34
  • 1
    @RichardCritten: I've never heard of any platform where `unsigned int` is 16 times the size of `char`. Probably you meant `CHAR_BITS * sizeof(unsigned int) == 16` or else `sizeof(unsigned int) == sizeof (uint16_t)` – Ben Voigt Jul 11 '22 at 15:36
  • @J.Doe by the way that code you linked does not calculate the power of two, but (as the name suggests) checks if a number is a power of two. For non-negative values, this should work independently of the size of integer. – Jakob Stark Jul 11 '22 at 15:42
  • @BenVoigt Thanks for the correction. Am having a bit of brain fade here. – Richard Critten Jul 11 '22 at 15:42
  • @Bathsheba technically I think you're right that all are dependent. Although `unsigned char` is always one byte since `sizeof(char) == 1` but bytes that are not octets may exist – perivesta Jul 11 '22 at 15:45

1 Answers1

0

a. Why can we store INT_MAX in an int variable, such as:

int res=INT_MAX;

INT_MAX is the maximum value that can be store in int. Per definition that can be stored in an int.

b. How is the code like below which calculates the power of 2 valid (runs without any error/warning): ...

because the constraints say: -2^31 <= n <= 2^31 - 1, shouldn't we be using long?

The constraints are wrong. First they assume int to be 32bit, which is not the case on e.g. AVR. Second they assume int is two's-complement, which is only true since C++20.

It would be more accurate to say the contraints are INT_MIN <= n <= INT_MAX. But really what is the point. The argument is int. That already says that.

Better would be to use unsigned int, uintmax_t or a template. There is no reason to limit this function to int.

Since C++20 there is std::has_single_bit for this.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
  • "*Second they assume `int` is two's-complement, which is only true since C++20*" - yes, it is only a *guarantee* since C++20. But in *practice*, most common implementations do use two's-complement. – Remy Lebeau Jul 11 '22 at 19:24