Multiplication and Division by 2 without using "*" or "/" operators in Java

Here is a simple way in Java to do a multiplication and division of a number by 2 with using arithmetic operators.

The trick is to use bitwise operator.
Here is a Java Code

Class Shimpu{
public static void main(String args[]){
int n = 2;
System.out.println("Multiplication by 2 " + n << 1);
n = 2;
System.out.println("Division by 2 " + n >> 1);

}
}
Shifting all the bits to left means multiplying by 2. 
Shifting all the bits to right means division by 2. 

Hope you like it. Please comment on any doubts.
Happy Learning !!!
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment