Tilde Operator in JavaThe bitwise complement operator falls under the category of the unary operator (deals with just a single operand). It takes one number and reverses all pieces of it. When a bitwise administrator is applied on bits, then, at that point, all the 1's turned into 0's and the other way around. The operator for the bitwise complement is ~ (Tilde). Example: Input: ~ 0000 0011 Output: 1111 1100 Input: 1110 0111 Output: 0001 1000 The bitwise complement operator ought to be utilized cautiously. The consequence of the ~ tilde operator on a modest number can be a major number on the off chance that the outcome is put away in an unsigned variable. Furthermore, the outcome might be a negative number on the off chance that the outcome is put away in a marked variable (expecting that the negative numbers are put away in 2's complement structure where the furthest left bit is the signed bit). Input: n = 2 Binary form of 2 = 0010 Tilde operator on 2 = ~ 0010 = 1101 The decimal value of 1101 is 13. Expected output: 13 Correct Output: 3 The 2's complement is returned by the compiler. Example1: program for working of tilde operatorOutput: Explanation: The tilde of 2 (~2) is  3 rather than 13. However, why? At the point when numbers are imprinted in base10, the consequence of a NOT activity can shock. Specifically, positive numbers can become negative and the other way around. How about we first track down the twofold portrayal of the bitwise complement of 2, which is  3 The negative numbers are sorted as the two's complement of the positive partner. 2's complement: The 2's complement is applicable to binary numbers. The 2's complement of a number is equivalent to the supplement of that number in addition to 1. Example: Tilde Operation of 2 (~ 0010) is: 1101 Now calculate the 2's complement of 3: The Binary form of 3 is = 0011 Now add 1 to the 1's complement of 3 = 1100 Adding 1 to 1's complement = 1100 +1 2's complement of 3 = 1101 Note: The bitwise Complement of 2 is same as the binary representation of 3
Example2: program to find 2's complementMain.java Output:
Next Topic8 Puzzle problems in Java
