4.2算術演算子
本のページ数:P 124コード名:unaryOps.cc overflow.cc arith-ex.cc int-div.cc
//unaryOps.cc
#include
using std::cout; using std::endl;
int main()
{
int i = 1024;
int k = -i; // i is -1024
bool b = true;
bool b2 = -b; // b2 is true!
cout << b << " " << b2 << " " << endl;
return 0;
}
// overflow.cc
#include
using std::cout; using std::endl;
int main()
{
short short_value = 32767; // max value if shorts are 16 bits
short_value += 1; // this calculation overflows
cout << "short_value: " << short_value << endl;
return 0;
}
// arith-ex.cc
#include
using std::cout; using std::endl;
int main()
{
cout << -30 * 3 + 21 / 5 << endl;
cout << -30 + 3 * 21 / 5 << endl;
cout << 30 / 3 * 21 % 5 << endl;
cout << 30 / 3 * 21 % 4 << endl;
cout << -30 / 3 * 21 % 4 << endl;
cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl;
return 0;
}
//int-div.cc
#include
using std::cout; using std::endl;
int main()
{
// ival1 is 3; result is truncated; remainder is discarded
int ival1 = 21/6;
// ival2 is 3; no remainder; result is an integral value
int ival2 = 21/7;
cout << ival1 << " " << ival2 << endl;
return 0;
}