bool表达式的值能安全地当成0和1吗?
我想弄一个掩码列,表示一系列条件的组合:
unsigned char state = 0;state |= a<b;state |= (c<d) << 1;state |= (e<f) << 2;//......if (state == 0xXX) {}else if (state == 0xYY) {}else if (state == 0xZZ) {}//......
union flags{ struct { bool state1: 1; bool state2: 1; bool state3: 1; bool state4: 1; }field; unsigned long mask;};flags f;f.field.state1 = a < b;f.field.state2 = c < d;f.field.state3 = e < f;cout<<f.mask<<endl;
[解决办法]
不知道是不是标准
不过vs和gcc中 ture都是1