Does the result of `bnot` for nonzero integers always flip the sign?

Viewed 54

Does the result of bnot for nonzero integers always flip the sign?

More precisely:

  1. Is bnot NonNeg where NonNeg >= 0 always a negative number?

  2. Is bnot Neg where Neg =< 0 always a non-negative number?

2 Answers

Seems like yes, except for -0. Also, it should be behaving like it does in C.

Eshell V11.1.7  (abort with ^G)
1> bnot 1.
-2
2> bnot -1.
0
3> bnot 55.
-56
4> bnot -55.
54
5> bnot 23456789543213456787654323456789765432.
-23456789543213456787654323456789765433
6> bnot -23456789543213456787654323456789765432.
23456789543213456787654323456789765431
7> bnot 0.
-1
8> bnot -0.
-1
9> 


In short - yes.

Reason for that is that Erlang uses U2 for encoding negative integers, which mean that bnot X is essentially equivalent to -X - 1.

Related