I came across this challenge on Edabit and couldn't work out this bitwise operation solution.
notNotNot = (a,b) => !!(a%2 >> b)
The challenge:
//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
I did some research that that operator:
Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.
That I reckon I understood (e.g. 5 >> 1
same as 0101 >> 1
which evaluates to 0010
), but I can't see how that works with a boolean? I know true
evaluates to 1
and false
to 0
.
Mixing bitwise and relational operators in the same full expression can be a sign of a logic error in the expression where a logical operator is usually the intended operator.
A bitwise operation operates on two-bit patterns of equal lengths by positionally matching their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both the first AND second bits are 1. If only one bit is a 1, the result is 0.
A little bit more into explaining how shift works with a boolean. So true as you said will be 1 and false will be 0.
Difference Between Bitwise and Logical Operators First, logical operators work on boolean expressions and return boolean values (either true or false), whereas bitwise operators work on binary digits of integer values (long, int, short, char, and byte) and return an integer.
The function you gave does not satisfy the challenge. Right shifting will not do what is asked for. For example, your notNotNot(6,true)
is false
, not true
when put through your function.
Your question is about bitwise operation on a boolean though. Since operators like >>
and <<
work on integers, Javascript first converts the boolean value to an integer. So true
becomes 1 and false
becomes 0. To see this you can shift by zero:
console.log("true is",true >> 0) console.log("false is", false >> 0)
Using !!
is a handy way to convert anything into a boolean. It takes anything that would be considered equivalent to false (such as 0, null
, undefined
or "") and gives back false
. Similarly anything that is truthy (like 14, "hello", [4], {a:1}) and give back true
. !!
works because the first exclamation mark gives the 'not' of the expression which is always true
or false
, then the second exclamation mark gives the opposite of that (false
or true
).
Getting back to the challenge, it wants to apply the not-operator 'a' times and compare to the 'b' value. So something like this would work:
function notNotNot(a, b) { return !!(a%2 - b); } console.log("notNotNot(1, true)",notNotNot(1, true)); console.log("notNotNot(2, false)",notNotNot(2, false)); console.log("notNotNot(6, true)",notNotNot(6, true));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With