programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Bitwise Inversion Operator

pelluru rao
Greenhorn
Posts: 5
Hello friends!!
This question is actually jargon.com
test3.html
the output of this programme is -7 .which is
unpredictable to me.
The ~(Bitwise Inversion Operator) inverts the
bit value.if so
for ~6 the output to be a large value but it is -7.
can any one give a suitable clue.
public class Test3Q40{
private void test() {
System.out.println(~6);
//00000000 00000000 00000000 00000110=6
}// 1111111111111111 11111111 11111001=?
static public void main(String[] a) {
new Test3Q40().test();
}
}
waiting for cordial replay.

Anonymous
Ranch Hand
Posts: 18944
~6 = (-6) -1 = -7
-7 is 1111 1111 1111 1111 1111 1001
how is this ? negative numbers are represented in 2's complement form. so above bit pattern is a 2's complement representation of -7.
Here is how you get the 2's complement of -7,
7 in binary = 0000 0000 0000 0000 0000 0111
1's complement of 7 = 1111 1111 1111 1111 1111 1000
add 1 to it = 1111 1111 1111 1111 1111 1001

pelluru rao
Greenhorn
Posts: 5
now it is crystal clear.
ThankQ vasansini.

 It is sorta covered in the JavaRanch Style Guide.