Java Program on Bitwise Compliment Operator

In the previous article we have discussed about Java Program on Logical NOT Operator

In this article we will see the use of Bitwise Compliment operator in Java programming language.

Java Program on Bitwise Compliment Operator

Bitwise Compliment operator is a unary operator which works with only one operand. It returns Compliment value as result for a given input value means it changes binary value 1 to 0 and 0 to 1. It is represented by the symbol ~ (called as tilde).

Syntax:

~operand

For Example:

Suppose, Number = 5 then its binary is 0101
Compliment of the number = ~0101 = 1010
So 1010 is 10 in decimal value.
But compiler will return -6 as compiler gives 2's Compliment as result.
When we add 1 with 1's Compliment we get 2's Compliment.

Program:

public class Main
{   
    public static void main(String[] args)   
    {   
        int x = 5;   
        // bitwise compliment  
        System.out.println("~x = " + (~x));   
    }  
}
Output:

~x = -6

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: