You are here:   HomeJava ProgrammingOperators & ExpressionBitwise Complement Operator Example in Java
by Dinesh Thakur Category: Operators and Expressions

The bitwise complement operator (~), which perform a bitwise negation of an integer value. Bitwise negation means that each bit in the number is toggled. In other words, all the binary 0s become 1s and all the binary 1s become 0s.

x = 8;

Y = ~x;

Integer numbers are stored in memory as a series of binary which can be either 0 or 1. A number is considered negative if the highest order bit in the number is set to 1. Because a bitwise complement converts all the bits in a number including the high order bit (sign bit). Since the number becomes negative, it is in 2's compliment form. To know its decimal value, first we will subtract 1 from the number and then perform l's compliment onto it (i.e. convert 1 to 0 and 0 into 1).

Here is the Java Example for Bitwise Complement Operator:

class BitwiseComplementOperator

{         
            public  static  void  main  (String  args[ ] )
            {
                        int  x  =  8 ;
                        System.out.println("x  =  "  +  x);
                        int  y  =  ~x;
                        System.out.println ("y  =  "  +  y);
            }
}

Bitwise Complement Operator



What's New and Popular