#include #include #include /*************************************************** * Fixed-width integer data types * * The sizes of the traditional C++ integer types * * short * int * long * long long * * are not completely standardized. * * For example, on the 64-bit Linux systems used for * this course, you will normally find * * sizeof( long ) == 8 * * while on 64-bit Windows systems you will normally * find * * sizeof( long ) == 4. * * Both are valid C++ implementations. * * Sometimes, however, we need an integer with a * specific number of bits. This is especially useful * when working with binary data, file formats, * communications protocols, or hardware. * * The library provides integer types whose * widths are explicitly specified. * * These include * * std::int8_t std::uint8_t * std::int16_t std::uint16_t * std::int32_t std::uint32_t * std::int64_t std::uint64_t * * The number in the name specifies the exact number * of bits used to represent the integer. * * Thus, * * std::int16_t * * is a signed integer type having exactly 16 bits, * while * * std::uint16_t * * is an unsigned integer type having exactly 16 bits. * * The 'u' in uint means unsigned. * * The suffix '_t' is commonly used in C and C++ names * to indicate that the name represents a type. * * * ARE THESE TYPES ALWAYS AVAILABLE? * * There is one subtle point. * * C++ provides std::int8_t, std::int16_t, and the * other exact-width types only if the implementation * has an integer type with exactly that width. * * On the computers you are likely to encounter, * these types will normally all be available. * * * BITS ARE NOT NECESSARILY BYTES * * The number in a type such as * * std::int32_t * * specifies its number of bits, not its number of * bytes. * * On our computers, one byte contains eight bits, so * * std::int8_t occupies 1 byte * std::int16_t occupies 2 bytes * std::int32_t occupies 4 bytes * std::int64_t occupies 8 bytes. * * This is what you will see when running this program * on our systems. * * * SIGNED AND UNSIGNED RANGES * * An exact-width signed integer having n bits has * the range * * -2^(n - 1) through 2^(n - 1) - 1. * * An exact-width unsigned integer having n bits has * the range * * 0 through 2^n - 1. * * For example, std::int8_t has the range * * -128 through 127, * * while std::uint8_t has the range * * 0 through 255. * * * std::numeric_limits * * As in the previous exercise, we can use * * std::numeric_limits::min() * * and * * std::numeric_limits::max() * * to determine the range of a particular integer * type. ***************************************************/ // Function declaration int main(); // Function definition int main() { std::cout << "There are " << std::numeric_limits::digits << " bits in a byte on this system." << std::endl << std::endl; ////////////////////// INT8_T and UINT8_T ///////////////////////////// std::cout << " >>>> 8-BIT INTEGERS <<<<" << std::endl << std::endl; std::cout << "A 'std::int8_t' is " << sizeof( std::int8_t ) << " byte(s)." << std::endl; // std::int8_t and std::uint8_t are commonly aliases // for signed char and unsigned char. Consequently, // std::cout may try to print their values as // characters rather than as numbers. // // The leading + promotes each value to an integer // type so that std::cout prints the numerical value. std::cout << "The type 'std::int8_t' ranges from " << +std::numeric_limits::min() << " to " << +std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'std::uint8_t' ranges from " << +std::numeric_limits::min() << " to " << +std::numeric_limits::max() << "." << std::endl << std::endl; ///////////////////// INT16_T and UINT16_T //////////////////////////// std::cout << " >>>> 16-BIT INTEGERS <<<<" << std::endl << std::endl; std::cout << "A 'std::int16_t' is " << sizeof( std::int16_t ) << " byte(s)." << std::endl; std::cout << "The type 'std::int16_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'std::uint16_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; ///////////////////// INT32_T and UINT32_T //////////////////////////// std::cout << " >>>> 32-BIT INTEGERS <<<<" << std::endl << std::endl; std::cout << "A 'std::int32_t' is " << sizeof( std::int32_t ) << " byte(s)." << std::endl; std::cout << "The type 'std::int32_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'std::uint32_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; ///////////////////// INT64_T and UINT64_T //////////////////////////// std::cout << " >>>> 64-BIT INTEGERS <<<<" << std::endl << std::endl; std::cout << "A 'std::int64_t' is " << sizeof( std::int64_t ) << " byte(s)." << std::endl; std::cout << "The type 'std::int64_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl; std::cout << "The type 'std::uint64_t' ranges from " << std::numeric_limits::min() << " to " << std::numeric_limits::max() << "." << std::endl << std::endl; return 0; }