#include <iostream>
using std::cout;
using std::endl;
int main()
{
int x = 0xAa; // 0x is normally followed by lowercase letters a..f
int y = 0XaA; // 0X is normally followed by uppercase letters A..F
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
/*
g++ Hex.cpp -o Hex
./Hex
x = 170
y = 170
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
float x = 5, y = 3;
// cout << "x % y = " << x % y << endl; // compile error
cout << "x % y = " << (int)x % (int)y << endl;
return 0;
}
/*
g++ Modulus.cpp -o Modulus
./Modulus
x % y = 2
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
double d1 = 1.6, d2 = 1.7;
cout << "d1 = " << d1 << ", d2 = " << d2 << endl;
int i1 = d1, i2 = d2;
cout << "i1 = " << i1 << ", i2 = " << i2 << endl;
cout << "i1 + i2 = " << i1 + i2 << endl;
int i = d1 + d2; // add doubles, truncate to int only after addition
cout << "i = " << i << endl;
cout << "d1 + d2 = " << d1 + d2 << endl;
cout << "(i1 == d1): " << (i1 == d1) << endl; // no auto truncation
cout << "(i1 == (int)d1): " << (i1 == (int)d1) << endl;
return 0;
}
/*
g++ Truncate.cpp -o Truncate
./Truncate
d1 = 1.6, d2 = 1.7
i1 = 1, i2 = 1
i1 + i2 = 2
i = 3
d1 + d2 = 3.3
(i1 == d1): 0 // false
(i1 == (int)d1): 1 // true
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
double d1 = 1.6, d2 = 1.7;
cout << "d1 = " << d1 << ", d2 = " << d2 << endl;
d1++; // works for floats too (not just for integers)
d2 += 2.1; // can use fractional values too, not just integral values
cout << "d1 = " << d1 << ", d2 = " << d2 << endl;
return 0;
}
/*
g++ Increment.cpp -o Increment
./Increment
d1 = 1.6, d2 = 1.7
d1 = 2.6, d2 = 3.8
*/