Showing posts with label bitwise operators. Show all posts
Showing posts with label bitwise operators. Show all posts

Sunday, 22 May 2011

&& vs & operator

Don't confuse &&, which is the short-circuit logical and, with &, which is the uncommon bitwise and. Altho the bitwise and can also be used with boolean operands, this is extremely rare and is almost always a programming error.

Saturday, 30 April 2011

Best Way to Swap Values in Java

Though not all Java applications require variables to be swapped, but those which do require are not given proper thoughts about correct and optimized implementation.


Method-1: Using temporary Variable

The most common way to swap two values is by using a temporary variable as shown below:

int a=10,b=5,c;
c=a; (c=10,a=10,b=5)
a=b; (c=10,a=5,b=5)
b=c; (c=10,a=5,b=10)

If the use of temporary variable c is not allowed, then we have the following two options:

Method-2.1 : Using the addition and subtraction operations
int a=10,b=5;
a=a+b;
b=a-b;
a=a-b

The problem with the above solution is that the addition and subtraction can cause overflows. An overflow is a condition when the value of a variable is increased/decreased beyond the maximum/minimum value of that variable. Though the output of such a program may show that the variables have been swapped but it is not guaranteed as per Specification of various programming languages including Java.

package com.vaani.swap.subtraction;
class Test{
    public static void main (String args[]) {
            int a=1999999999,b=1899999999;
            a = a + b;
            System.out.println("After operation 1, a = " + a +
                 " b = " + b);
            b = a - b;
            System.out.println("After operation 2, a = " + a +
                 " b = " + b);
            a = a - b;
            System.out.println("After operation 3, a = " + a +
                 " b = " + b);
   }
}

Output:
After operation 1, a =  -394967298 b = 1899999999
After operation 2, a =  -394967298 b = 1999999999
After operation 3, a =  1899999999 b = 1999999999

Method-3: Using the XOR operator

The XOR operator can also be used swap two variables using the logic shown below:

int a=10,b=5;
a=a^b;
b=a^b;
a=a^b

Thank you

Tuesday, 26 October 2010

Bitwise Operators

Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is shorter than an int, it is promoted to int before doing the operations.

It helps to know how integers are represented in binary. For example the decimal number 3 is represented as 11 in binary and the decimal number 5 is represented as 101 in binary.
Negative integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111 1111 1100.

Bitwise operators can fall under 2 categories.>>,>>>,<< operators are called shift operators, which is covered in this post. Other operators are ~,&,| and ^ operators.
~ Not operator
^ Xor or Exclusive OR
| OR
& And operator
>> Right shift
>>> Unsigned right shift
<< left shift

Note that operands for Bitwise operators must be numeric datatypes(char, short, int, or long).

Examples - The bitwise operators

Operator Usage Example Result Reason
& or and Operator a & b 3 & 5 1 1 if both bits are 1.
| OR operator a | b 3 | 5 7 1 if either bit is 1.
^ or xor operator a ^ b 3 ^ 5 6 1 if both bits are different.
~ or not operator ~a ~3 4 Inverts the bits.
Left shift n << p 3 <<< 2 12 Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.
Right shift n >> p 5 >> 2 1 Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit is shifted into the high-order positions.
Unsigned right shift n >>> p -4 >>> 28 15 Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.