While including the header <limits>
and calling std::numeric_limits<uint64_t>::max()
will solve the question,
here is the simplest way of actual implementation using typecasting, in case that you don't remember the spelling of std::numeric_limits<uint64_t>::max()
. (Amnesia can actually happen to all of you! ...at some point in the past or in the future...)
- Note: The usability of this method depends on how your language version treats the negative value. The "two's complement" is the issue around 2023... but you have to try this once, if you haven't tried.
It writes:
uint64_t(-1) // functional notation
or
(uint64_t)-1 // c-like cast notation
For example, you can print the exact value of its max number like this:
#include <iostream>
int main() {
std::cout << "max value = " << uint64_t(-1);
}
and the result of output is:
max value = 18446744073709551615
Voilà!
==== further reading below ====
Indeed, many compiler headers use this kind of typecasting as the value of those pre-defined "limits" macro. For example, in my macOS dev environment with XCode and g++, my C++ header file limits.h says, inside the template <class _Type, bool = is_arithmetic<_Type>::value> class __libcpp_numeric_limits
:
static constexpr const bool is_signed = _Type(-1) < _Type(0);
it's a clever way of checking the signed-unsigned type. And then,
static constexpr const _Type __min = __libcpp_compute_min<_Type, digits, is_signed>::value;
static constexpr const _Type __max = is_signed ? _Type(_Type(~0) ^ __min) : _Type(~0);
the min uses the calculation that involves a special template, while the max uses is_signed result. The min template is:
template <class _Type, int __digits, bool _IsSigned>
struct __libcpp_compute_min {
static constexpr const _Type value = _Type(_Type(1) << __digits);
};
which looks similar to your expression in your question (1<<64)-1
except the -1
part.
Try checking the limits headers in your own computer and see what it says!
#define __STDC_LIMIT_MACROS
is, but you probably want it before the includes, not before use ofUINTwhatever
– Motormancstdint
which providesUINT64_MAX
. Boost also provides an implementation of this header, if it is available to you. – Debugstd::numeric_limits<uint64_t>::max()
work? – Feilnumeric_limits
specialization for those types, although chances are good that there is one. Although,max()
is less useful withoutconstexpr
. – Debugstd::numeric_limits<uint64_t>::max()
did the trick. Thanks. You can put it as an answer if you want to. – Yoursnumeric_limit::max()
to beconstexpr
. But you're right in that some popular implementations are non-conforming. – Feil#ifndef UINT64_MAX #define UINT64_MAX 18446744073709551615
, with the relevant suffix (ifull
is not supported, then perhapsui64
) – Merry#define UINT64_MAX = 0xFFFFFFFFFFFFFFFFULL
. – Kite