Tilde Operator in Java17 Mar 2025 | 3 min read The 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 base-10, 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
Example-2: program to find 2's complementMain.java Output: ![]() Next Topic8 Puzzle problems in Java |
Java Static Type Vs Dynamic Type Java is a strongly-typed language that categorizes variables, expressions, and objects into static types. However, Java also supports dynamic typing through the use of its object-oriented features. In this section, we will explore the concepts of static and dynamic typing in...
5 min read
? In Java, there are mainly three classes related to the String. The classes are String, StringBuilder, and StringBuffer class. These three classes provide methods related to string manipulation. Removing the first and last character from the String is also an operation that we can perform on...
6 min read
The Java.nio.DoubleBuffer has a rewind() function. To rewind this buffer, utilize the DoubleBuffer Class. If a position was iously marked, it will be discarded. This method resets the position to zero while maintaining the limit. When it is necessary to perform a number of channel write...
3 min read
Java, a widely used programming language, offers a plethora of data structures to facilitate efficient and flexible coding. While arrays are fundamental and frequently used, they come with their own set of disadvantages. In this section, we will explore some of the limitations of arrays in...
24 min read
The Codility Passing Cars problem is just one of the many typical algorithmic problems in which the actual goal is to determine the total number of valid pairs of cars that are traveling in opposite directions on the same road. More specifically, the problem requires calculating...
4 min read
Java is a well-known computer language that has been in use for more than two decades. Several essential features enable developers to construct efficient and elegant code. For example, the List sort() function is used to sort a List in ascending or descending order. In this...
4 min read
The switch case in Java has undergone some modification to add some new features in more recent versions of Java. In this tutorial, we are going to discuss the switch case in Java 12. However, before that, let's see an example that shows the implementation of...
3 min read
Fonts are a fundamental aspect of any graphical user interface, and Java provides robust support for handling and displaying fonts. Whether we develop a desktop application using Swing or a web application with JavaFX, understanding how to work with fonts is essential for creating visually...
9 min read
? Adding a date to a MySQL database from a Java application is a common requirement when working with date-related data. In this article, we will guide you through the process of adding a date in MySQL using Java. We will cover the necessary steps, including establishing...
4 min read
Shadowing of static functions in Java is the phenomenon of having two static methods in the same scope with the same name. The first method is said to be shadowed by the second method. The second method will take precedence over the first method when it...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India