Thread: Bit wise oeperators adition , subtraction , division and multiplication

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    Bit wise oeperators adition , subtraction , division and multiplication

    Hi ,

    I have plan to write a programs for adition,subtraction , division and multiplication between two operands by using bitwise operators with out +,-,*and /.

    Is there any systematic way to create these logics?

    I looked at one example for multiplication as below by googled. by seeing that , i need to "byheart" :-) that logic to remember.

    without by heart is there any easy systematic way to do all those things?

    Code:
    #include<stdio.h>
    int main()
    {
            int a,b,result;
            printf("nEnter the numbers to be multiplied :");
            scanf("%d%d",&a,&b);
            printf ("\n");
            result=0;
            while(b != 0)               // Iterate the loop till b==0
            {
                    rintf ("value of a=%d b=%d \n",a,b);
                    if (b&01)                // Logical ANDing of the value of b with 01
                    {
                            printf ("value of a=%d b=%d \n",a,b);
                            result=result+a;     // Update the result with the new value of a.
                            printf ("result=%d\n",result);
                    }
            a<<=1;                   // Left shifting the value contained in 'a' by 1.
            b>>=1;                   // Right shifting the value contained in 'b' by 1.
            }
            printf("nResult:%d\n",result);
            return 0;
    }
    Please let me know any suggestion

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just one... Trying to memorize your way through programming is not going to work...
    You really do need to understand what's going on before you'll be any good at it.

    FWIW there are a couple of errors in your code...
    In your scanf() near the top, you probably want a space between the %d markers.
    You are missing the p at the beginning of the first printf() in your loop.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nkrao123@gmail. View Post
    without by heart is there any easy systematic way to do all those things?
    Sure, it's the same as learning multiplication normally: if you can understand how (and why) multiplication works, it's easier than memorizing a table. And/or it makes memorizing the table easier.

    So you found the algorithm:

    Code:
    int product (int a, int b) {
    	unsigned int x, y, product = 0;
    	int sign = 1;
    
    	if (a < 0) {
    		sign *= -1;
    		x = -a;
    	} else x = a;
    	if (b < 0) {
    		sign *= -1;
    		y = -b;
    	} else y = b;
    
    	while (y > 0) {
    		if (y & 1) product += x;
    		x <<= 1;
    		y >>= 1;
    	}
    
    	return sign*product;
    }
    Th purpose of the preliminary bit with setting sign is because signed integers use the highest bit to indicate negativity. If you shift a signed integer value around, its signedness will be affected. We need to use unsigned numbers which are pure power of 2 bit values, but we also need to restore the signedness of the product at the end. So if one but not both of the factors are negative, sign will be -1. Pretty simple.

    Now to the real question: how does the part in red work?

    Let's use 10 * 10, which takes 4 passes thru the loop, as an example. Ten in binary is 1010. Every time you shift left, you are multiplying by 2. Everytime you shift right, you are dividing by two (and rounding down, because the Least Significant Bit = 1 and it is shifted off).

    On the first pass, y & 1 == false, so "product" is still 0, y is shifted right to become 5, and x is shifted left to become 20.

    On the second pass, y & 1 == true, so product becomes 20, y becomes 2, and x becomes 40.

    On the third pass, y & 1 == false, so product remains 20, y becomes 1, and x becomes 80.

    On the fourth pass, y & 1 == true, so product becomes 100, y becomes 0, and x becomes 160.

    That's how, but it doesn't explain why this "duplicates" what we think of as multiplication. But remember, shifting left is multiplication, by two, aka squaring or raising to the power of 2. "y & 1" just tests to see if the lowest bit in y is set. What that function does is take every bit set in y and add the corresponding power of 2 value of x. If the first bit is set, we add just x. If the second bit is set, we add x^2. If the third bit is set, we add x^4.

    I would never have thought that algorithm up myself and my mathematical vocabulary is limited, so my explanation might be incomplete, but hopefully that still sheds some light.
    Last edited by MK27; 09-04-2011 at 10:18 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The algorithm is the same as you learned in school, because when you face it multiplication is multiplication:
    Code:
       185
     x 243
    ------
       555
      740
     370
    ------
     44955
    but simplified because the only digits in binary are 0 (in which case we don't have to add anything) or 1 (in which case we just add the top number). The left shift moves us over so that we are lined up correctly in the bottom table (one space for each digit over), and the right shift gets us through all the digits in the second number in turn, from right to left.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer subtraction
    By bhrugu in forum C Programming
    Replies: 10
    Last Post: 08-07-2010, 02:04 PM
  2. Division using repeated subtraction
    By dbzx in forum C Programming
    Replies: 10
    Last Post: 05-27-2009, 06:07 PM
  3. float multiplication and subtraction
    By panos in forum C Programming
    Replies: 4
    Last Post: 07-16-2008, 01:29 AM
  4. Division By Subtraction
    By blacksnake in forum C++ Programming
    Replies: 19
    Last Post: 01-13-2007, 10:17 AM
  5. product using adition
    By blacksnake in forum C++ Programming
    Replies: 32
    Last Post: 01-05-2007, 05:11 PM