码迷,mamicode.com
首页 > 其他好文 > 详细

ch5-使用表达式、 语句和运算符

时间:2018-06-29 23:35:03      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:xor   std   sam   内存   赋值   理论   art   amp   main   

#include <QCoreApplication>
#include <iostream>
#include <bitset>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

#if 0
    //1.
    cout << "Hello             World!" << endl;

    cout << "Hello "
            "World!" << endl;

    //2.
    //左值通常是内存单元,另外,右值可以是内存单元里的内容
    uint daysInYear = 365;

    //3.
    cout << 20 / 3 << endl;
    cout << 20.0 / 3 << endl;
    cout << 20 % 3 << endl;
    //invalid operands of type ‘double‘ and ‘int‘ to binary ‘operator%‘
// cout << 20.0 % 3 << endl;

    //4.
    //递增运算符和递减运算符、前缀和后缀
    //首先需要理解前缀和后缀之间的差别,这样才能选择合适的方式。使用后缀运算符时,先将右值赋给左值,再将右值递增或递减
    //前缀运算符的行为完全相反,即先将右值递增或递减,再将结果赋给左值
    int startValue(100);

    int postfixIncrement = startValue++;
    cout << "postfixIncrement:" << postfixIncrement << ",startValue:" << startValue << endl;
    startValue = 100;
    int prefixIncrement = ++startValue;
    cout << "postfixIncrement:" << postfixIncrement << ",startValue:" << startValue << endl;

    //您经常会听到前缀运算符的性能更高还是后缀运算符性能更高的争论。换句话说,++startValue 优于 startValue++。
    //至少从理论上说确实如此,因为使用后缀运算符时,编译器需要临时存储初始值,以防需要将其赋给其他变量。
    //就整型变量而言,这对性能的影响几乎可以忽略不计,但对某 些类来说,这种争论也许有意义。
    //聪明的编译器可能通过优化消除这种差异。
    startValue++; //is the same as ...
    ++startValue;

    //5.
    //逻辑运算符:与或非
    int n1(10), n2(20);
    bool isLessEquals = n1 <= n2;
    cout << "isLessEquals:" << isLessEquals << endl;

    cout << "enter true(1) or false(0) for two operands:" << endl;
    bool op1 = false, op2 = false;
    cin >> op1 >> op2;

    cout << op1 << " AND " << op2 << " = " << (op1 && op2) << endl;
    cout << op1 << " AND " << op2 << " = " << (op1 || op2) << endl;
#endif

    //6.
    //按位运算符
#if 0
    cout << "input a number(0,255):" << endl;
    quint8 inputNum = 0;
    cin >> inputNum;

    bitset<8> inputBits = inputNum;
    cout << inputNum << " in binary is " << inputBits << endl;

    bitset<8> bitwiseNOT = ~inputBits;
    cout << "~ " << inputBits << " = " << bitwiseNOT << endl;

    bitset<8> bitwiseOther = 0x0F;
    bitset<8> bitwiseAND = inputNum & bitwiseOther.to_ulong();
    cout << inputBits << " AND " << bitwiseOther << " = " << bitwiseAND << endl;

    bitset<8> bitwiseOR = inputNum | bitwiseOther.to_ulong();
    cout << inputBits << " | " << bitwiseOther << " = " << bitwiseOR << endl;

    bitset<8> bitwiseXOR = inputNum ^ bitwiseOther.to_ulong();
    cout << inputBits << " | " << bitwiseOther << " = " << bitwiseXOR << endl;

    cout << "before inputnum:" << inputBits << endl;
    inputNum |= bitwiseOther.to_ulong();
    inputBits = inputNum;
    cout << "after inputnum:" << inputBits << endl;
#endif

    //7.
    //按位右移运算符,按位左移运算符
#if 0
    int number = 0;
    cout << "input a number:" << endl;
    cin >> number;

    int halfNum = number >> 1;
    cout << "half num:" << halfNum << endl;

    int quarterNum = number >> 2;
    cout << "quarter num:" << quarterNum << endl;

    int doubleNum = number << 1;
    cout << "double num:" << doubleNum << endl;

    int quadrupleNum = number << 2;
    cout << "quadruple number:" << quadrupleNum << endl;
#endif

    //8.
    //复合赋值运算符
// bool ba(false), bb(false);
// ba &&= bb;

    //9.
    //sizeof
    int intArr[100] = {0};
    cout << sizeof(intArr) << endl;
    cout << sizeof(int) << endl;
    cout << sizeof(intArr[0]) << endl;

    //务必使用括号让代码和表达式易于理解。
    //务必使用正确的变量类型,确保它不会溢出

    return a.exec();
}




ch5-使用表达式、 语句和运算符

标签:xor   std   sam   内存   赋值   理论   art   amp   main   

原文地址:https://www.cnblogs.com/splitfire/p/9245670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!