×

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

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Thread Safety and How to Achieve it in Java

Before diving into the topic, let’s just recap the concept of Multithreading provided by Java where we can create and execute multiple threads of the same object. When these multiple...

5 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Normal and Trace of a Matrix in Java

Normal of a matrix The square root of the total squares of each element in a matrix is known as the matrix's normal. Think of the following matrix as an example. Example: [[1,2,3]  ...

3 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 add 4 Hours to the Current Date in Java?

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

2 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.