#include <iostream>
#include <cstdint>
using namespace std;
int main()
{
cout << "true to int is " << static_cast<uint16_t>(true) << ", false to int is " << static_cast<uint16_t>(false) << endl;
if (static_cast<uint16_t>(true) == 1) {
cout << "true to int is 1" << endl;
} else {
cout << "true to int is NOT 1" << endl;
}
if (static_cast<uint16_t>(false) == 0) {
cout << "false to int is 0" << endl;
} else {
cout << "false to int is NOT 0" << endl;
}
return 0;
}
输出如下:
$ ./bool_to_uint.exe
true to int is 1, false to int is 0
true to int is 1
false to int is 0