c++ - 检查 XOR 是否也适用于语句,以及 C++ 中的 OR
问题描述
目前,如果元素是声明的开始或语句的开始,我有以下函数返回布尔值。
bool start_of_block_element() {
return start_of_declaration() || start_of_statement();
我需要检查这些的异或是否也为真并输出一个布尔值。我不确定如何将它们组合在一起。如果 XOR 和 OR 都返回 true,它应该返回 true
我的猜测是:
bool start_of_block_element() {
return (
(start_of_declaration() ^ start_of_statement() ) && ( start_of_declaration() || start_of_statement() )
);
}
这是正确的方法吗?
解决方案
假设bool D = start_of_declaration()
和bool S = start_of_statement()
你想要D || S == true
和D ^ S == true
。所以基本上,
D | 小号 | 返回
--+---+----
0 | 0 | 0
1 | 0 | 1
0 | 1 | 1
1 | 1 | 0
任何给出此真值表的运算符都将满足您的要求,因此请使用具有此真值表的运算符:
return start_of_declaration() != start_of_statement()
推荐阅读
- python - 我希望它在不是 0 或 1 时给我错误
- amazon-web-services - 如何确保每个 aws lambda 的不同公共 IP
- c++ - 在 Release 中作为 QImage 加载时,相同的图像具有不同的像素数据
- oracle - 使用 Jenkins 构建和部署 Oracle Forms 10g?
- android - 无法从 Ubuntu 18.10 访问 Redmi Note 5 Pro
- android - 将 .jar 依赖项添加到 Java 库模块 Android Studio
- android - 使用 AndroidX 时未找到辅助 dex 文件中的 MultiDexApplication 类
- java - PositionalDataSource 和recyclerview。为什么在 invalidate() 之后滚动?
- hive - hive 中 2 个带纳秒的时间戳数据之间的差异
- javascript - jQuery addClass 没有效果