-2

I am new at this portal, I am searching for Bitwise complement of (-4) or any negative number in Java. Can anyone help me out of this?

1 Answers1

0

Just use the bitwise complement operator ~ like

int complement = ~ -4;

it inverts every bit, that is, a 0 bit turns into 1, and 1 in a 0.

Here the Tutorial and the Specification

If the question is about negative numbers and so, that is, why the result of ~ -4 is 3, try this Entry (summary: negative numbers are represented by two's complement of the positive one, not just the plain complement {one's complement})

user85421
  • 28,957
  • 10
  • 64
  • 87