測試項目:
1.bool 變量的 四則運算
2.bool 變量所在內存大小
測試代碼:
[cpp]
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7. int iSizeCouner = 0;
8. bool bTestBool = false;
9. bool a = 3;
10. bool b = 1;
11. bool c = a + b;
12. bool d = a - b;
13.
14. // test
15. cout << "a = " << a <<endl;
16. cout << "b = " << b<<endl;
17. cout << "c = " << c<<endl;
18. cout << "d = " << d <<endl;
19.
20. iSizeCouner = sizeof(bTestBool);
21.
22. cout << iSizeCouner <<endl;
23. cout << bTestBool <<endl;
24. cout << bTestBool <<endl;
25.
26. bTestBool = true;
27. cout <<boolalpha<<bTestBool <<endl;
28.
29. while(1);
30. }
打印輸出:
a = 1
b = 1
c = 1
d = 0
iSizeCouner = 1
bTestBool = 0
結論:
1.在四則運算中始終都是 0,1 參與運算
2.bool 變量 所占字節大小 為1 字節
摘自 DriverMonkey的專欄