0

I have a int a=255=>1111 1111 after negating i.e ~a=10000 0000=>-64
isnt it suposed to be 0000 0000=>0
basically want to ~255 to 0 like in 8 bit binary format i.e
1111 1111 to 0000 0000 with out making it negative.

Dhroiden
  • 53
  • 9

1 Answers1

5

The problem is that a is probably of type int, which in Java takes up 4 bytes. So what you're getting is:

1111 1111 1111 1111 1111 1111 0000 0000

You should just mask the result, if you know that you're always going to work with 8 bits:

int b = ~a ^ 0xFF;
PaF
  • 3,297
  • 1
  • 14
  • 15