I have 3 separate Boolean variables, bit1
, bit2
& bit3
and I need to calculate the decimal integer equivalent in JavaScript?
Using the method booleanObjectMethodToInt, we can convert a boolean value to an integer the same way we did with the static method.
To convert boolean to integer in python, we will use int(bool) and then it will be converted to integer.
The compare() method of Java Boolean class compares the two Boolean values (x and y) and returns an integer value based on the result of this method.
+true //=> 1 +false //=> 0 +!true //=> 0 +!false //=> 1
Ternary operator is a quick one line solution:
var intVal = bit1 ? 1 : 0;
If you're unfamiliar with the ternary operator, it takes the form
<boolean> ? <result if true> : <result if false>
From Sime Vidas in the comments,
var intVal = +bit1;
works just as well and is faster.
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