Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# inversion operator

Rally Krish
Greenhorn
Posts: 5
What gets printed when the following program is compiled and run. Select the one correct answer.

public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}

pls expain

vidya sagar
Ranch Hand
Posts: 580

3 when represents in 8 bit(Because it is byte).It will be

3 = 0000 0011
~3 = 1111 1100

Binary 1111 1100 when converted to decimal it results in -4

Rally Krish
Greenhorn
Posts: 5
Thanks

Binary 1111 1100 when converted to decimal it results in -4

can u pls expalin the conversion

vidya sagar
Ranch Hand
Posts: 580
Operators

Rally Krish
Greenhorn
Posts: 5
vidya sagar,

studying, i got struck here "-6 in binary is 11111010"

again i need to know how exactly the conversion is taking place

Thanks

Henry Wong
author
Marshal
Posts: 21754
85
Originally posted by Rally Krish:
vidya sagar,

studying, i got struck here "-6 in binary is 11111010"

again i need to know how exactly the conversion is taking place

Thanks

Java uses two's complement to represent integers. But to summarize the link -- to negate a number, the two's complement operation is to invert all the bits, and add one.

Henry