(Source: www.geeksforgeeks.org)

Contents

The unary bitwise complement operator ~ inverts a bit pattern; it can be applied to any of the integral types, making every “0” a “1” and every “1” a “0”. The following program, Video, uses the bitwise AND operator to print the number “2” to standard output.

Usually, a programmer works on variables of data types like int, float, double, String or collections like Array, List, Arrays and so on. Languages like Java and C# let you manipulate data at the bit level, which means you have access to a specific bit in a byte.

In this topic, we are going to learn about Bit Manipulation in Java. There are many other applications of Bit manipulation that we going to see later in this article.

Java supports 3- bit shift and 4 bitwise operators to perform operations at the bit level. These operators can be used on integral types (int, short, long and byte) to perform operations at the bit level.

1) Bitwise OR This is a binary operator which takes 2 operands and denoted by the symbol “| “. The bitwise operator compares the corresponding bits of the two operands.

(Source: www.youtube.com)

Public class Bitwise {public static void main(String arms) {int operand1 = 15, operand2 = 27, output = operand1 | operand2; System.out.print(output);} } The bitwise operator compares the corresponding bits of the two operands.

Public class Bitwise {public static void main(String arms) {int operand1 = 15, operand2 = 27, output = operand1 & operand2; System.out.print(output);} } 3) Bitwise Complement Unlike the other two operators which we have discussed so far, this takes only one operand and denoted by the symbol “~”.

Note: When you run the Java program you will get -16 as an output instead of 240 because compiler shows 2’s complement of that number i.e. negative notion of the binary number. In our case the 2’s complement of operand 15 is -16 which is the output of the program as well.

The left shift operator is denoted by the symbol “<<”. Java enables you to manipulate integers on a bit of level, that means operating on specific bits, which represent an integer number.

Turns out there is another, a slightly less known set of operators, which manipulate numbers on bit level. This fancy name basically means a bit negation.

(Source: www.softpedia.com)

It takes every single bit of the number and flips its value. Because 42 is int, it is represented as a 32- bit value, that is 32x ones or zeros.

So all the positions to the left of 101010 are actually filled with zeros up to 32 bits total. A & B means that all the bits of both numbers are compared one by one and the resulting number is calculated based on values of the bits from numbers A and B. Bitwise AND is similar to logical AND in a sense that it results in 1 only when the two compared bits are both equal to 1.

That shows that shifting a number by one is equivalent to multiplying it by 2, or more generally left shifting a number by n positions is equivalent to multiplication by 2^ n. To understand the difference, it is necessary to know how negative numbers are represented in Java.

Binary representation on its own does not provide information whether the number is negative. There needs to be a special rule to define how to represent negative numbers in binary.

Signed right shift moves all the bits by given the number of positions to the right. Unlike the signed shift, the unsigned one does not take sign bits into consideration, it just shifts all the bits to the right and pads the result with zeros from the left.

(Source: java.dzone.com)

Signed and unsigned right shifts have the same result for positive numbers. Java offers a shorter syntax for assigning results of arithmetic or bitwise operations.

Bit manipulation can be very handy in some cases and is really efficient. The readability suffers a lot at it can be really puzzling for somebody who is not familiar with the bit manipulation concept.

Don't use bit manipulation everywhere possible just because you learned a cool new concept. Here, the expressions (salesclerk == 1) and (saleswoman >= 10000.0) are evaluated separately.

Notice the use of parentheses to clarify where one expression ends and another begins. Using parentheses isn’t always necessary, but when you use logical operators, it’s a good idea to use parentheses to clearly identify the expressions being compared.

Because both expressions compared by the & operator must be true for the entire expression to be true, there’s no reason to evaluate the second expression if the first one returns false. About the Book Author Doug Lowe began writing programming books before Java was invented.

(Source: designs.generalfinishes.com)

They can be used with any of the integral types (char, short, int, etc). They are used when performing update and query operations of Binary indexed tree.

Shift Operators: These operators are used to shift the bits of a number left or right thereby multiplying or dividing the number by two respectively. They can be used when we have to multiply or divide a number by two.

An FAQ is available here. Select the file according to your operating system from the list below to get the latest Java for your computer. » FAQ about 64- bit Java for Windows Java allows you to play online games, chat with people around the world, calculate your mortgage interest, and view images in 3D, just to name a few.

It's also integral to the intranet applications and other e-business solutions that are the foundation of corporate computing. In Java, bitwise operators perform operations on integer data at the individual bit -level.

The bitwise OR | operator returns 1 if at least one of the operands is 1. The following truth table demonstrates the working of the bitwise OR operator.

(Source: www.slideshare.net)

The bitwise AND & operator returns 1 if and only if both the operands are 1. The following table demonstrates the working of the bitwise AND operator.

The bitwise XOR ^ operator returns 1 if and only if one of the operands is 1. The following truth table demonstrates the working of the bitwise XOR operator.

However, it is important to note that we cannot directly convert the result into decimal and get the desired output. To understand this we first need to calculate the binary output of -36.

The left shift operator shifts all bits towards the left by a certain number of specified bits. Java 1 bit Left Shift Operators we can see from the image above, we have a 4-digit number.

As a result, the left-most bit (most-significant) is discarded and the right-most position(least-significant) remains vacant. When we shift any number to the right, the least significant bits (rightmost) are discarded and the most significant position (leftmost) is filled with the sign bit.

(Source: www.slideshare.net)

Here, we are performing the right shift of 8 (i.e. sign is positive). So the leftmost bits are filled with 0 (represents positive sign).

Here, the vacant leftmost position is filled with 0 instead of the sign bit. As we can see the signed and unsigned right shift operator returns different results for negative bits.

09: Different Girths