C++ integer types a tutorial !

The integer types in C++ are fundamental types. A fundamental type , has a mapping to hardware , so the operations performed on a fundamental type , are hardware performed .

Boolean , Characters , and integer types , are called the integral types. The integral types and the floating point types are called the arithmetic types .

What are the C++ integer types

An integer type is a whole number, as in -1or 1. A whole number does not contain a fractional part , such as .2.

Integer types can either be unsigned or signed . An unsigned integer , can only be non negative , for example 0, or 1, whereas a signed integer , can be negative , positive , or zero , as in -1, or 3.

C++ standards , prior to C++20 , do not specify the algorithm for representing the signed , or unsigned integer types . A signed integer , can be represented , using one’s complements , two’s complement , or sign and magnitude . C++20 , specifies that signed integers , must be represented using two’s complement .

The C++ standards dictates that unsigned arithmetic , such as addition , or subtraction , must obey arithmetic modulo two , to the power n, this is related to overflow . Overflow happens , when the result is too large to fit in the allocated width , for an integer type .

An integer type , can have multiple type specifiers, for example : int, signed int, and signed, represent the same integer type int, which is signed . This being said , an integer type can be declared in multiple ways , or using multiple keywords.

It is clear , from the preceding list , that for signed integer types , it is not necessary to use the keyword signed.

A signed integer type , and its unsigned version , such as int, and unsigned int, have the same storage size and alignment . Alignment is where in memory , an object can be placed .

An implementation can define other integer types, called the extended integer types . For each extended integer type , a signed , and an unsigned version , must be defined .

What are the C++ integer types ranges

The C++ standard specifies the minimum ranges, that a signed or an unsigned integer type , might have , an implementation , can define larger ranges .

Before C++ 20 , and because signed integer types could have , a sign and magnitude representation , the minimum ranges were :

Starting C++20 , the minimum ranges are :

The minimum ranges of the integral types , as implemented on a given machine , are defined by the C++ header climits.

The sizeof operator , returns the number of bytes, which are reserved for a given type , hence it can be used to get the number of bytes , reserved for integer types .

C++ integer literals types

An integer literal such as 17, can be written in base 2, 8, 10, or 16, as follows :

Since C++ is a typed language , an integer literal has a type. An integer literal is always non negative , the negation operator -, is applied on the gotten integer literal type .

Decimal integer literals , have a default type ofint, if too large to fit in an int, they will have a long type , if too large to fit in a long, they will have the long longtype . If still too large to fit , and the implementation defines extended integer types , they are tried , as described , if still too large , the behavior is implementation defined .

Binary , octal , and hexadecimal integer literals , have a default type ofint, if too large to fit in an int, they will have a type of unsigned int, if too large to fit in an unsigned int, they will have a type of long, next unsigned long, next long long, next unsigned long long, next if the implementation defines extended integer types , they are tried as stated , if still too large , the behavior is implementation defined .

The suffixes l, and ll, case insensitive , can be used with an integer literal , to state that it is of type long, or long long. In such cases , and to determine the type of the integer literal , the compiler starts from long, or long long, depending on the suffix , and try the next types , as described earlier .

The suffix u, case insensitive , can be applied to an integer literal , to state that it is unsigned . In such case , unsigned intis first tried , followed by unsigned long, followed by unsigned long long. If still too large , and the implementation defines extended integer types , the extended integer types are tried , as stated , if still too large , the behavior is implementation defined .

The suffix ucan be used with the suffixes l, and llto state that an integer literal is unsigned long, or unsigned long long. In such a case , the compiler tries , the next larger unsigned type , if the literal is too large to fit ,and if no unsigned integer type can fit the literal , then the behavior is implementation defined .

C++ Standard library integer types

The integer types defined by the C++ standard , are defined to have a least width , as such a least range , so the range of a standard integer type , is not uniform across all implementations .

For example, on a 16bit architecture , inthave typically a width of 16bits , whereas on a 32bits architecture , inthas typically a width of 32bits .

The question to ask is as such , what if what was needed , is to have a fixed length width , for an integer type , across all implementations ?

Thestandard library header cstdint, defines fixed width integer types, they are :

The fixed width integer types , are optional, so it is not necessary for an implementation to provide them .

What about , if what was needed , is an integer type , for which a processor in an execution environment , so where the program is being executed , is faster to perform operations, and this integer type , is to be of a minimum length ? The standard library cstdintheader , defines the following integer types , that fit these requirements :

Finally what if what was needed , is to have the largest integer type , available on an implementation . To fulfill , this requirement , the cstdintheader defines :

Originally published at https://twiserandom.com on February 22, 2021.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store