我在Edabit上遇到了这个挑战,无法解决这个按位运算解决方案。
notNotNot = (a,b) => !!(a%2 >> b)
挑战:
//Something which is not true is false, but something which is not not true is true!
//Create a function where given n number of "not", evaluate whether it's true or false.
//Examples:
notNotNot(1, true) ➞ false
// Not true
notNotNot(2, false) ➞ false
// Not not false
notNotNot(6, true) ➞ true
// Not not not not not not true
我对该操作员进行了一些研究:
通过从左侧推入最左边的位向右移动,并让最右边的位掉落。
我认为我理解了(例如5 >> 1与0101 >> 1计算结果相同0010),但是我看不到布尔值如何工作?我知道true对1和false进行评估0。
notNotNot(2, true):它将返回false,但不是真的(!!true)应该是true...
false要解析为的奇数个not true。
notNotNot = (a,b) => !!((a%2)^b)...
(a, b) => !!((a + b) % 2)?
true=1(十进制)=01(二进制)左移一位将产生10二进制或2十进制。