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?
Asked
Active
Viewed 398 times
-2
-
Why not just print it out and see for yourself? β BartoszKP May 19 '18 at 07:16
-
1[Why is βCan someone help me?β not an actual question?](http://meta.stackoverflow.com/q/284236) β Andrew Thompson May 19 '18 at 07:16
1 Answers
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