[erlang-questions] unary bitwise not

Richard O'Keefe ok@REDACTED
Thu Mar 5 01:57:30 CET 2009


Thinking about bitwise operations in terms of bytes is a good
way to get confused.  Binary numbers go *all* the way to the
left.
The bottom line is that bnot X = -1 -X,
so I don't need to look at the bits to tell that bnot 153
*should* be -154.

(Hint: -1 is an infinite number of 1 bits, going *all* the way
to the left.  Subtracting a binary number from that bit pattern
doesn't involve any borrows.  In each column, 1 - 1 => 0, 1 - 0 => 1,
so this really does flip all the bits.)




More information about the erlang-questions mailing list