6.5 INTEGER TYPES


6.5 INTEGER TYPES

C++ and Java have in common the following integer types:

      short      int      long 

In C++, typically a short has two bytes of memory, an int four bytes, and a long eight bytes. From the standpoint of portability of software, it is important to note that the C++ standard says nothing about the exact number of bytes to be used for each. All that is stipulated by the C++ standard is that the memory allocated to a short be at least as large as the memory assigned to a char, the memory assigned to an int be at least as large as the memory assigned to a short, and that the memory assigned to a long be at least as large as the memory assigned to an int.

By contrast, Java stipulates that a short have exactly two bytes, an int four, and a long eight. Java also supports one additional integer type:

      byte 

which, as its name implies, gets one byte for its storage. As with the above three types, byte represents a signed integer and its value ranges from -128 to 127.

In addition to the three integer types mentioned previously, C++ also supports signed and unsigned types. A signed int is synonymous with a plain int. The unsigned integer types are ideal for uses that treat storage as a bit array.

Constant integer values are referred to as integer literals. For example, the integer 234 is an integer literal. For both C++ and Java, integer literals come in three forms:

      decimal      octal      hexadecimal 

the most common being the decimal form. An integer literal starting with 0 followed by digits between 0 and 7 is an integer in octal form. An integer literal starting with Ox followed by hex digits is an integer in hexadecimal form. Here are some examples of integer literals:

decimal:

0

7

81

2345

octal:

00

07

0121

04451

hexadecimal:

0x0

0x7

0x51

0x929

Octal and hexadecimal notations are most useful for expressing and visualizing bit patterns. To illustrate, the int 81 is stored in the memory as the following 4-byte bit-pattern in the big-endian representation:

      00000000 00000000 00000000 01010001 

The uppermost four bits of the last byte have a decimal value of 5 and the very last four bits have a decimal value of 1, leading to a hex representation of 0x51 for the 4-byte bit-pattern. One can much more readily visualize the bit-pattern associated with the hex 0x51 than with the equivalent decimal 81. For an even more difficult example, consider the int 2345 in the above table. It is difficult to visualize the bit pattern corresponding to the four bytes that will be occupied by this integer in the memory. However, dividing repeatedly by 16, we can quickly convert the integer into its hex representation-0x929-and write down the following bit pattern by concatenating the 4-bit patterns associated with each of the hex digits:

      00000000 00000000 00001001 00101001 

Hex and octal representations are also useful for creating memory dumps. If you wanted to dump out the contents of a segment of memory in a byte-by-byte manner, you could read each byte as a char through a binary stream and print it out as a hexadecimal int. That way you could get a hardcopy map of what's in the memory.

An integer literal is of type long if it is suffixed with either the letter L or its lower-case version l.




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net