-1

What does the "~" operator do in java as in: byte b =~5 + 1 ?

When the result is sent to standard out System.out.print(b) it prints '-5' but I don't know why or what it represents.

Jimbo
  • 25,790
  • 15
  • 86
  • 131

1 Answers1

6

It's the bitwise complement operator. Since java uses two's complement to represent negative numbers, ~x + 1 is equivalent to -x.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469