×

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
01
10

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.


Related Topics

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

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...

3 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Zigzag Array in Java

In this tutorial, we discuss, what is zigzag array and its example. Even we will create the java program. In this program, we convert the simple array into a zigzag...

4 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Split String into String Array in Java

The String split() technique returns a variety of divided strings after the strategy parts the given String around matches of a given normal articulation containing the delimiters. The ordinary articulation...

4 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

InputMismatchException in Java

What is InputMismatchException? One of the most frequent errors in Java is the InputMismatchException. Because the InputMismatchException is a subtype of the java.lang, it is an unchecked exception. RuntimeException.Because it is...

4 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.