Type | Decimal | Octal | Hexadecimal |
---|---|---|---|
int | 16 | 020 | 0x10 |
int | 255 | 0377 | OXff |
int | 32767 | 077777 | 0x7FFF |
unsigned int | 32768U | 0100000U | 0x8000U |
int (32 bit-) long (16 bit-CPU) | 100000 | 0303240 | 0x186A0 |
long | 10L | 012L | 0xAL |
unsigned long | 27UL | 033UL | 0x1bUL |
unsigned long | 2147483648 | 020000000000 | 0x80000000 |
The following code shows how to display hexadecimal integer literals and decimal integer literals.
#include <iostream> using namespace std; int main() /*from w w w. java 2 s . c o m*/ { // cout outputs integers as decimal integers: cout << "Value of 0xFF = " << 0xFF << " decimal" << endl; // Output: 255 decimal // The manipulator hex changes output to hexadecimal // format (dec changes to decimal format): cout << "Value of 27 = " << hex << 27 <<" hexadecimal" << endl; // Output: 1b hexadecimal return 0; }
Integral numerical constants can be represented as simple decimal numbers, octals, or hexadecimals:
You can designate the type of a constant by adding the letter L or l (for long), or U or u (for unsigned). For example,
Value | Meaning |
---|---|
12L 12l | correspond to the type long |
12U 12u | correspond to the type unsigned int |
12UL 12ul | correspond to the type unsigned long |