Integral:
Floating:
char
float
short
double
int
long double
long
enum
unsigned
char
unsigned
short
unsigned
int
unsigned
long
|
|
|
|
|
| char | 1 | -128 | 127 |
| unsigned char | 1 | 0 | 255 |
| short | 2 | -32768 | 32767 |
| unsigned short | 2 | 0 | 65535 |
| int | 2 | -32768 | 32767 |
| unsigned int | 2 | 0 | 65535 |
| long | 4 | -2,147,483,648 | 2,147,483,647 |
| unsigned long | 4 | 0 | 4,294,967,295 |
Examples of octal constants:
0
056
07777
Examples of octal constants:
0x10
0x56
0xFFFF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 * 100
= 5 * 1 =
5
+ 4 * 101
= 4 * 10 = + 40
+ 3 * 102
= 3 * 100 = + 300
+ 2 * 103
= 2 * 1000 = + 2000
+ 1 * 104
= 1 * 10000 = + 10000
= 12345
F * 160
= 15 * 1 =
15
+ A * 161
= 10 * 16 = + 160
+ 2 * 162
= 2 * 256 = + 512
+ 1 * 164
= 1 * 4096 = + 4096
= 4783
Examples:
123
is a signed int(by default)
123U
is an unsigned int
123L
is a signed long
123UL
is a unsigned long
Examples of floating constants:
|
|
|
Remarks |
| 6.83 | double | Floating point constants are double by default |
| 6.83F | float | Explicit float constants end in F or f |
| 6.83L | long double | Explicit long double constants end in L or l |
| 4.35E-9 | double | Exponential notation meaning 4.35X10-9 . |
Example:
char n = 0x5A; // 01011010 binary
a = n << 1;
// 10110100
b = n >> 1;
// 00101101
c = n >> 4;
// 00000101
Example:
char n = 5; // 00000101 binary
a = n << 1;
// 00001010 == 0x0A == 10
b = n >> 1;
// 00000010 == 0x02 == 2
c = n << 4;
// 01010000 == 0x50 == 80
|
|
|
|
|
Bitwise AND |
|
|
Bitwise OR |
|
|
Bitwise EXCLUSIVE OR |
|
|
Complement (invert all bits) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char n = 5;
// 00000101 binary
char m = 6;
// 00000110 binary
a = n & m;
// 00000100 == 0x04
b = n | m;
// 00000111 == 0x07
c = n ^ m;
// 00000011 == 0x03
d = ~n;
// 11111010 == 0xFA
Examples:
// Determine if n is an odd number
if (n & 0x0001 == 0x0001)
. . .
else
. . .
// Make n an even number
n = n & 0xFFFE;
// Make n an odd number
n = n |0x0001;
// Swap the contents of m and n
// without using another variable
m = 0x2A; //
00101010
n = 0xA5; //
10100101
m = m ^ n; // 10001111
n = m ^ n; // 00101010
= 0x2A
m = m ^ n; // 10100101
= 0xA5
Example:
is equivalent to:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n += x = 7;
associates from right to left. Therefore it first stores 7 in x and then adds that value to n.
enum Days
{SUN, MON, TUE, WED, THU, FRI, SAT};
The value of SUN is 0, MON is 1, TUE is 2, etc.
enum Month {JAN
= 1, FEB, MAR, APR, MAY,
JUN, JUL, AUG, SEP, OCT,
NOV, DEC};
The value of JAN
is 1, FEB is 2, etc.
Days today;
today =
TUE;
Examples:
Days today;
today =
TUE;
today =
Days(today + 1); // today++ won't work
for (today
= SUN;
today <= SAT;
today = Days(today + 1)
{
cout << today << endl;
}
outputs the numbers 0, 1, 2, 3,
4, 5, 6; not the literals.
Examples:
if (today
== TUE)
. . .
if (today
< FRI)
. . .
Enumerated values can be used with
a switch statement:
enum Days
{SUN, MON, TUE, WED, THU, FRI, SAT};
Days today;
int
weekday;
. . .
switch (today)
{
case SUN:
case SAT:
weekday = FALSE;
break;
default:
weekday = TRUE;
}