c - How to apply NXOR on 2 bits in c
问题描述
I have two bits in C language and I want o perform bitwise NXOR operator on them. I.e:
a=0,b=0 or a=1,b=1 -> NXOR(a,b)=1
a=1,b=0 or b=1,a=0 -> NXOR(a,b)=0
Any easy way to perform this? I know that the XOR operator in C is ^
, but can't figure an easy way to apply the NXOR operator.
解决方案
Make it from bitwise xor ^
and bitwise not ~
. For example:
unsigned nxor(unsigned x, unsigned y) {
return ~(x^y);
}
C only has OR, AND, NOT, and XOR. Any other bitwise operator you have to build from those four. Fortunately, you can make any bitwise operator from those four.
If you care about the high bits you can mask them off.
unsigned nxor_1bit(unsigned x, unsigned y) {
return 1 & ~(x^y);
}
推荐阅读
- node.js - 有没有办法在 AWS CodeBuild 托管映像中升级 nodejs 10?
- rust - 在 Rust 中使用泛型时解决冲突 trait 实现的策略?
- racket - Racket 的评估顺序有多确定?
- xslt - XSLT - 多输入 XML,多输出 XML
- r - R中的正则表达式:拆分字符串并提取数字
- java - 从 JSON 字符串中的任何位置删除键而不进行反序列化
- javascript - 有人能发现问题吗?只有第一个选项适用于选择
- ios - 从“等待审核”中删除应用
- java - 打印数组的内容只返回 [null, null, null, null]
- sparql - SPARQL - 使用纬度和经度从 DBpedia 获取城市名称