Additional Information on Numeric Types and Expressions


Numeric types

The definition of Java is machine independent — all of the numeric types have strict definitions. But C++ is different. The exact meaning of C++ programs depends to some extent on the machine where the program runs. A particular operating system defines a word size: a 64-bit operating system uses 64 bits per word, and a 32-bit operating system uses 32 bits per word. Xlogin.cs.ecu.edu is a 64-bit machine.

The following are numeric types in C++.

long

Values of type long are integers that are 64 bits long in a 64-bit program and 32 bits long in a 32-bit program. Use them for integers that might be fairly large. The largest integer that can be represented in 32 bits is roughly 2 billion, and the largest integer that can be represented in 64 bits is roughly 8 billion billion.

int

Values of type int are typically 32-bit integers.

short

Values of type short are typically 16-bit integers. The largest integer that you can store in a 16-bit integer is roughly 32,000.

char

Type char is quite different in C++ from the corresponding type in Java. C++ type char is more like Java type byte.

Values of type char are stored in a single byte (8 bits). You can store numbers from −128 to 127.


unsigned int, unsigned long, unsigned short, unsigned char

Normally, integers have a sign bit that is used to tell whether the integer is positive or negative. An unsigned integer is always treated as nonnegative, and does not devote one of the bits to the sign. So you can store a larger integer in an unsigned variable. A variable of type unsigned char can store a value from 0 to 255. The largest unsigned 32-bit integer is roughly 4 billion.

double

A value of type double is an approximation to a real number that typically uses 64 bits, even on a 32-bit machine. (The name double comes from the use of two words. But that is only historical. A 64-bit machine typically uses 64-bits for a value of type double.) That gives you about 15 (decimal) digits of precision.

Numbers are stored in base 2, not base 10, and you need to watch out for roundoff error. For example, 0.1 (one-tenth) cannot be represented exactly in base 2 with a finite number of digits, so it is rounded, similarly to the way 1/3 (0.33333...) is rounded in based 10.


float

A value of type float is an approximation to a real number that typically uses 32 bits. That gives you about 7 digits of precision. Because computers can perform many millions of operations in a single computation, roundoff error becomes a serious problem, and most programmers prefer to use type double for most real numbers.


Integer overflow

Each size of integer (given by a number of bits used to represent it) has a largest allowed positive value. If you add 1 to that number, you will not get an error. Instead, the result will be a negative number. For example, the largest value that can be stored in a variable of type short is 32,767. Statement

  short n = 32767;
  short m = n + 1;
makes m = −32768, the smallest negative number that can be stored in a variable of type short.


Numeric constants

234

Write an integer constant in the usual way. Do not write commas in a number.

123456789L

Normally, an integer constant is assumed to have type int. Add L after the number to indicate that it has type long. Other suffixes are U (unsigned int) and UL (unsigned long).

234.5

Write real numbers in the usual way. Note that 3 has type int but 3.0 has type double.

234.5E12

An E indicates a power of 10. Constant 234.5E12 is 234.5×1012 and 1.0E−30 is 1.0×10−30.

0xA2

Precede an integer constant by 0x to indicate that it is written in base 16 (hexadecimal). The digits are 0,1, ...,9,A,B,C,D,E,F, where A stands for 10, B stands for 11, etc. You can use either upper case A-F or lower case a-f as digits.

011

An integer constant that starts with 0 is assumed to be written in base 8 (octal). For example, 011 is the same as 9.