Posts

Showing posts from February, 2013

Multiplication of 2 integers using bits.

Hi everyone, this post will describe about how to multiply 2 integers using bits. The advantage in multiplying using bits, is it takes less time than simple multiplication. Consider two numbers 8 and 7, when we are required to multiply both numbers, we will just do like 8 * 7. We can also implement this multiplication using bit shifts. If 8 is the first number and 7 is the second number, we just need to return sum of 8 left shifted four times + 8 left shifted 2 times + 8, i.e, we can return 8<<2 + 8<<1 + 8. Shifting the number one time to left will double the number (When the bit reaches 32nd position, it wont double as it shifts to the 0th position) For knowing more about bit shifts, please refer my previous post - Bit shifts . Like this we can multiply an integer with every other integer. Here, I am presenting a few examples. public class MultiplyBits { public static void main (String args[]) { int num =8; //System.