Tilt operator in Java | Tilde operator in Java Example
The symbol designates it as a unary operator (pronounced as the tilde). It gives back the bit's complement or inverse. Every 0 turns into a 1, and every 1 back into a 0. It is symbolised by the character "~".
The ASCII character set includes the tilde (), also referred to as the squiggly or twiddle, which is used in both writing and programming. On keyboards, it is often situated below the Esc key. ASCII code 126 is utilised in a variety of IT-related applications in addition to being used in URL addresses, file systems, math, directories, programming, etc. The tilde character may appear as seen in the supplied image when input. In some operating systems, like Unix, the tilde symbol also denotes the home directory of the current user. On web servers, the first character in each user's You will frequently see the tilde character in Web URLs because users put their personal or business web pages under their personal home directory on the servers. When used in pairs, the tilde in mathematics signifies "roughly." It is sometimes referred to as the "equivalency sign" by mathematicians. The tilde character is the most popular standard for electronic text interchange. It serves as a bitwise operator in regular expression pattern matching and a unary negation in C programming.
Making the ~ character in a U.S. keyboard
On the American keyboard, you must simultaneously press the Shift key and the tilde key (), while holding down the Shift key. The tilde symbol is located below the Esc key in the top-left corner of the keyboard, on the same key as the backquote (').
What does the computer tilde character do?
- It can also be used to represent a unary negation as a bitwise operator in C programming languages.
- On a Linux computer, it can also be used to represent the home directory for the current user.
- In programmes and games like Quake, the console can also be accessed using the tilde character.
- Tilde can also be used to open the Tilda GTK+ terminal emulator.
- By putting it above a = sign, for instance. This means that it denotes the similarity of shapes.
- A different estimate, like "roughly equal to," can also be expressed with the double tilde.
A | ~A |
0 | 1 |
1 | 0 |
Example 1
Input - 00011100
Output - 11100011
Example 2
Input - 110100101
Output - 001011010
Explanation
In the above example after using the tilde operator we can see that all the 0’s are converted to 1’s and all the 1’s are converted to 0’s.
It's important to utilise the bitwise complement operator correctly. If an unsigned variable is used to store the result of an operator on a small integer, the result could be a huge number. If the result is saved in a signed variable and negative integers are stored with the leftmost bit serving as the sign bit, then the result could be a negative number.
Example
Input N = 3
3 in binary form is 0011
A bitwise complement operation o3 2 results in the decimal number 12 (~0011 = 1100 ).
Output : 12
Correct Output : -4
The 2's complement of the input value is returned by the compiler.
Explanation
In the above example the input number is 3 and its binary representation is 0011 and after using tilde operator (~) 0 is converted to 1 and 1 is converted to 0. The output is 1100.
Tilde.java
import java . util . * ;
// public class named Tilde
public class Tilde
{
public static void main ( String [ ] s )
{
// creating object for scanner class
Scanner sc =new Scanner ( System . in ) ;
// taking input using scanner class
System . out . println ( " Enter the number " ) ;
int a = sc . nextInt ( ) ;
// bitwise compliment
// ~0011= 1100 = -4
// printing the compliment
System . out . println ( "~a = " + ( ~a ) ) ;
}
}
Output
Enter the number
4
~a = -5
Explanation
In the above example the input value is taken using scanner class and compliment operator is performed on it.