Integer Numbers

Integer variables or constants can take on only positive and negative integral values. Four integer types are available, as shown in Table 2.2.

All the integer types are stored internally as two's complement. A positive number is stored as its corresponding binary representation. For example, the byte representation of the number 3 will be 00000011. To store a negative number, all the bits of the corresponding Positive binary number is inverted; then 1 is added to the result. For example, to obtain the byte representation of the number –4, we start with the binary representation of 4, 00000100. We invert the bits, resulting in 11111011. Finally, we add 1, resulting in 11111100. In this scheme, the sign is stored in the leftmost (high) bit: a zero indicating a positive number, and a one indicating a negative number.

Numbers larger than 1 byte are stored in big-endian order. The high-order (or most significant) byte is stored first in memory. Little-endian order follows the reverse convention. For example, take the short (2 byte) representation of the number 256:
00000001 00000000.

Positive binary number is inverted; then 1 is added to the result. For example, to obtain the byte representation of the number –4, we start with the binary representation of 4, 00000100. We invert the bits, resulting in 11111011. Finally, we add 1, resulting in 11111100. In this scheme, the sign is stored in the leftmost (high) bit: a zero indicating a positive number, and a one indicating a negative number.

Numbers larger than 1 byte are stored in big-endian order. The high-order (or most significant) byte is stored first in memory. Little-endian order follows the reverse convention. For example, take the short (2 byte) representation of the number 256:
00000001 00000000.

Int values are assigned as decimal values by default, as in

int i = 17;

To assign an octal value, prefix the value with a zero. For example,

int ioctal = 010;

assigns octal 10 (decimal 8) to ioctal.

To assign a hexadecimal value, prefix the value with a zero then an x. For example,

int ihex = 0xB;

assigns hexadecimal B (decimal 11) to ihex.

A long literal value has an l or L suffix, for example,

long lvar = 123456789L;

No comments:

Post a Comment