Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

The `ULL` integer literal suffix

`ULL` gives an integer literal the type `unsigned long long` when its value fits, which can change overload selection or how a bit shift is evaluated.

Version: ISO C++23; implementation support may vary

Syntax

integer-literal = (decimal-literal | octal-literal | hexadecimal-literal | binary-literal) ull-suffix
ull-suffix = unsigned-suffix long-long-suffix | long-long-suffix unsigned-suffix
unsigned-suffix = u | U
long-long-suffix = ll | LL

Attach the suffix directly to the digits, without whitespace. Examples: 42ULL, 052ull, 0x2ALLu, 0b101010uLL.

The unsigned long long literal suffix (ULL) gives an integer literal the type unsigned long long when its value fits. Attach it without spaces to a decimal, octal, hexadecimal, or binary numeral. It combines u or U with ll or LL, in either order. 42ull and 42LLu are valid, but 42UlL is not: the two l letters must have the same case.

42 is an int, 42U is an unsigned int, 42LL is a long long, and 42ULL is an unsigned long long. These literals have the same value but different types. The suffix sets the type at the literal itself; it does not convert the result of a finished expression. That's why kind(42) and kind(42ULL) select different overloads, each with an exact parameter-type match.

unsigned long long has at least 64 value bits, but it can have more. std::uint64_t, when available, is exactly 64 bits wide and need not be an alias for unsigned long long. If a ULL literal's value does not fit in unsigned long long, the implementation may use an extended unsigned integer type that can represent it. If no permitted type can represent it, the program is ill-formed; the literal does not wrap to a smaller value.

In 1ULL << 63, the left operand makes the result an unsigned long long. Shifting by 63 is valid because 63 is below that type's minimum width. A shift count that is negative or at least the width of the promoted left operand causes undefined behavior. Assigning the result to a wider type does not change the type in which the shift was performed.

Examples

#include <iostream>

const char* kind(int) { return "int"; }
const char* kind(unsigned long long) { return "unsigned long long"; }

int main() {
    std::cout << kind(42) << '\n';
    std::cout << kind(42ULL) << '\n';
    std::cout << (1ULL << 63) << '\n';
}

Result

int
unsigned long long
9223372036854775808

The first two lines identify the selected overloads. The last line shows the decimal value of `2^63`, obtained by shifting `1ULL` left by 63 positions.

Related lessons

  • C++ · Lesson 32 · Advanced · Version: 3

Sources