# Thread: multiplying by 321 without using * or / operators

1. ## multiplying by 321 without using * or / operators

I'm trying to write a program that multiplies any given number by 321 without using the built in multiplication or division operators (cannot do x * 321 or x / (1/321) ). I think I am going to have to use the bitwise operators, and then combine some sort of a mask using some bitwise operator. If anyone has done this before I'd really appreciate some help. I tried finding a pattern in the sequence of bits as it relates x (the number I'm multiplying) and x multiplied by 321. I could not find any static mask that I can use in all situations. I think I am going to have to calculate the mask, i.e I'm going to have to use some sort of an equation to create a mask and then use the mask to multiply x by 321? This is pretty confusing stuff Does it seem like I'm remotely on the right track? I feel lost.

2. Hint:

can you think of three powers of 2 that add up to 321?

3. 1) A loop containing additions

2) Bitshifts combined with additions. Note that 321 = 256 + 64 + 1, all three powers of 2.

3) x + x + x + x + x + ... + x + x (321 times).

4. Dont you people have any original homework anymore?

If you really want to make it interesting, add '+' to the list of operators you can't use.

5. I think it's
rslt = (value << 8) + (value << 6) + 1;

EDIT: This doesn't make sense, and it's how I got what I wrote above
Hint

To multiply by 20, you would do
rslt = (value *16) + (value *4);

Which can be written as
rslt = (value <<4) + (value<<2);
if I want to multiply 3 by 20, then we have:
rslt = (3 << 4) + (value << 2);
which is the same as
rslt = (3 * 2)^4 + (3 * 2) ^2
which does not yield 60, it yields:
1332
Unless I'm missing something, in which you'll kindly point it out (and not insult me)

6. no *, no /, no +, no loop

Code:
```#include <iostream>
using namespace std;

int main()
{
int x;
cout << "enter an integer > ";
cin >> x;
cout << (x|x<<8|x<<6);
return 0;
}```
but it only works for inputs of 1 through 4. Otherwise, replace (x|x<<8|x<<6) with (x+(x<<8)+(x<<6))

7. rslt = (3 << 4) + (value << 2);
which is the same as
rslt = (3 * 2)^4 + (3 * 2) ^2
which does not yield 60, it yields:
1332
not true:
(3 << 4) is the same as 3*(2^4) = 48

8. rslt = (value << 8) + (value << 6) + 1;
Should be:
rslt = (value << 8) + (value << 6) + value;

9. Multiply by 321 without using a whole bunch of ops you would normally think to use

Code:
```#include<stdio.h>

int increment( int blah ) {
unsigned int i;
for (i = 1; i; i <<= 1) {
if (!(blah & i)) {
blah |= i;
break;
} else {
blah ^= i;
}
}
return blah;
}

int add ( int a, int b ) {
b = ~b;
while ( (b=increment(b)) != 0 ) a = increment( a );
return a;
}

int mul321 ( int value ) {
int result = 0;
result = add( result, value << 8 );
result = add( result, value << 6 );
result = add( result, value      );
return result;
}

int main ( ) {
int i;
for ( i = 0 ; i < 20 ; i++ ) {
printf( "%2d %5d %5d\n", i, i*321, mul321(i) );
}
return 0;
}```
Writing
int decrement ( int blah );
int sub ( int a, int b );
Is left as an easy exercise for the reader