Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does bitwise operation work on Booleans?

Tags:

javascript

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.

like image 652
uber Avatar asked Apr 12 '20 07:04

uber


People also ask

Do bitwise operators work with Booleans?

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.

How does Bitwise operation work?

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.

Which number represents false in Bitwise operation?

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.

What is the difference between bitwise operations and Boolean logic operations?

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.


1 Answers

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)
So bitwise operation on booleans is really just bitwise operation on either 0 or 1.

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));
like image 65
Always Learning Avatar answered Sep 29 '22 15:09

Always Learning