Operator and Expression
 
The complement operator
 
1. Is unary (has a single operand)?
2. Uses the ~ operator to "flip" (reverse) the bits within an integer value. If a bit is "on" it is     turned "off". If a bit is "off" it is turned "on".
 
For example,
if
byte a = 17; // Binary value: 0001 0001 Hex value: 11
byte b;
after executing the statement
b = (byte) ~a;

variable b will have a value of -18 (because 0001 0001 reverses to 1110 1110). Once again, the cast is needed in order to store the int value that results from the operation.
 
Example:
The following program can be run to test complement operations.
 
import java.util.*;
import java.io.*;
public class Aman
{
public static void main(String[] args)
{

// Variable to be read from the user


// Prompt for and read an integer value

System.out.print("Integer: ");
Scanner Keyboard=new Scanner(System.in);
int number;
number=Keyboard.nextInt();


// Display the result of complementing the number

System.out.println(" ~" + number + " = " + ~number);
}
}
Output