0

When I deal with logical operator,I came up some questions.

I tried my work like below code.

a:true

b:false

then

(a|b):true (1)

but

~(a|b):-2

why this operator returns -2? and what is -2?

I guess this operator means not (a|b)therefore I expected to return 0

If someone has opinion,please let me know.

Thanks

let a = (4>1)
let b = (3<1)

console.log("a:",a);
console.log("b:",b);
console.log("(a|b):",(a|b));
console.log("~(a|b):",~(a|b));

if((a|b)){
console.log("#")
}

if(~(a|b)){
console.log("#2")
}
Heisenberg
  • 4,787
  • 9
  • 47
  • 76
  • 1
    Unless you're dealing with bits, I'd suggest not using bitwise operators, they're confusing – CertainPerformance Apr 12 '20 at 01:36
  • 1
    These are bitwise operators, not logical operators. Your example is sort of obfuscated but it boils down to `~(1)`. So is your question what is the `~` operator? – ggorlen Apr 12 '20 at 01:38
  • If the m.s.b. of a signed binary number is 1 then it is a negative number and ~ makes the leading 0s into 1s making the resulting number negative. See https://en.wikipedia.org/wiki/Two's_complement for why 11111110 is -2 – QuentinUK Apr 12 '20 at 01:39
  • 1
    Your guess is incorrect, ~ inverts the bits, ! is the not operator. – QuentinUK Apr 12 '20 at 01:43
  • `~(a|b)` -> `~(00000001)` -> `11111110` -> `-2` in base ten. We get negative 2 because negatives are represented using two's compliment – Nick Parsons Apr 12 '20 at 02:04

1 Answers1

0

Its a bit verbose because its maybe over commented but heres my answer in summary.

Theres two issues here.

The second one is simple. Javascript uses the exclamation mark to express NOT. "!true==false" whereas "~true==WTF?"

The first one took me a little to get my head round when I learned it. Theres two different OR operators.

A single "|" is used for bitwise oprations like

5 | 4 = 5 (because 5 is composed 1 + 4)
6 | 1 = 7 (becasue 6 is composed 2 + 4 and the 1 makes 1+2+4=7)

The double "||" is used for logical operations such that every value operated on is regarded as a boolean.

true || 0 = true
true || 12345 = true
123 || 456 = true
0 || null = false

The & also has its equivalent && to distinguish between bitwise and logical operations.

In short the "|","&" operators always return integer values and the "||","&&" always return boolean values.

Your code adjusted verbosely:

let a = (4>1);   //resolves to boolean true (4 is greater than 1)
let b = (3<1);   //resolves to boolean false (3 is not less than 1)

console.log("a:",a);   //(logs a boolean value of a (true)
console.log("b:",b);   //(logs a boolean value of b (false)


//"bitwise OR" operation on the integer values (a=0x0001,b=0x0000)
//which just luckily resolved to 0x0001 which resolves to true in boolean context
console.log("(a|b):",(a|b));

//"logical OR" operation on the boolean values (a=true,b=false)
console.log("(a||b):",(a||b));

//incorrect NOT operator for javascript "~"
console.log("~(a|b):",~(a|b));

//correct NOT operator for javascript "!" using "bitwise OR" on integers
console.log("!(a|b):",!(a|b));

//correct NOT operator for javascript "!" using "logical OR" on booleans
console.log("!(a||b):",!(a||b));



if((a|b)){
console.log("#")
}

if(~(a|b)){
console.log("#2")
}
Stephen Duffy
  • 467
  • 2
  • 14