### Thread: Binary equivalent of decimal place shift

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
2
Rep Power
0

#### Binary equivalent of decimal place shift

Hi all,

I am trying to do the following:

1. There are two numbers 'a' and 'b', lets say a = 221, b = 72.
2. I converted them to binary such that a = 11011101 and b = 1001000
3. Now I am trying to combine them such that the final result is 72221 but in binary.
4. I tried to shift 'b' by 8 bits (number of bits in a) and add with 'a' but did not get the required result.

Regards
2. Originally Posted by PKJack123
Hi all,

I am trying to do the following:

1. There are two numbers 'a' and 'b', lets say a = 221, b = 72.
2. I converted them to binary such that a = 11011101 and b = 1001000
3. Now I am trying to combine them such that the final result is 72221 but in binary.
4. I tried to shift 'b' by 8 bits (number of bits in a) and add with 'a' but did not get the required result.

Regards
What is the exact criterion in order to combine numbers? putting the smaller number before the bigger one? If you convert each of the numbers separately into binary before their combination, why not doing the same thing for the combination? that is, first calculating in decimal the value of their combination based on the algorithm (that as I said you haven't specified in the OP) and finally convert it to the binary?

Regards,
Dariyoosh
3. IOW, you want to multiply b by 1000 and then add a to that.

A decimal left-shift would require you to multiply by 10. Do that three times to convert 72 to 72000. I know of no binary-shift method to do that -- which doesn't mean that one doesn't exist, just that I know of none.

{EDIT: last statement caveat: excluding the binary method for multiplying by 10}

PS

Here's an alternative method:
Code:
```    int b = 72;
int a = 221;
int x;   // assuming this isn't in Turbo C (ie, a 32-bit int and not 16-bit)
char s[10];

sprintf(s, "%d%d", b, a);  // "72221"
sscanf(s, "%d", &x);  // x == 72221```
Though I suspect that's not what your instructor wants.

Part of our problem is that we don't know what the requirements of your assignment are. You do need to multiply by ten to perform a left decimal shift. Since this is a C forum, we assume that you can simply multiply it by 10. Are you also required to implement binary multiplication? -- (see Binary Multiplication) We need to be told these things.

PPS
BTW,
4. I tried to shift 'b' by 8 bits (number of bits in a) and add with 'a' but did not get the required result.
If you shift b by 8 bits, what are you doing? You are multiplying b by 2 to the 8'th power, which is 256. Why were you trying to do such a thing, which has nothing to do with your goal? What were you thinking? Or were you just panicking and trying random nonsense in the vague hope that something might happen?

No! You need to stop and think! And if you try something, you need to think beforehand of what you are trying to do. Obviously, multiplying by 256 would not work.

Obviously, doing a decimal left shift once requires that you multiply by ten. What are your requirements for doing that multiplication?
Last edited by dwise1_aol; February 21st, 2013 at 01:32 PM.
4. Assuming that you live in a far different timezone, I'll streamline this by assuming that you are indeed required to implement binary multiplication. I will furthermore assume that you do not understand binary multiplication and that the Wikipedia link I gave you just went over your head. If none of those assumptions is true, then disregard this message.

Binary multiplication is exactly like in decimal: a series of multiply, add, shift. Only because you're multiplying the multiplicand by either zero or one, binary multiplication is simpler, a series of add and shift -- ie, if the muliplier bit is one, you add the shifted multiplication to the product, but if it's zero then you don't add.

Here's an example multiplication problem in decimal:
Code:
```        12345
× 123
--------
37035
246900
1234500
-------
1518435```
In that problem, 12345 is the multiplicand, 123 is the multiplier, and 1518435 is the product.

Drawing on my schooling in 1960's "new math", here's what's actually happening there:
12345×3 + 12345×20 + 12345×100 = 1518435
But 12345×20 is the same as 12345×(2×10) is the same as (12345×10)×2 is the same as 123450×2. So the problem can be rewritten as:
12345×3 + 123450×2 + 1234500×1 = 1518435
in which the multiplicand is shifted left for each successive digit. Which is what the long multiplication method uses, so for each successive digit of the multiplier, going from right to left, the procedure is:
1. Initialize the product to zero and start with the least significant digit of the multiplier.
2. Multiply the multiplicand by the selected digit in the multiplier.
3. Add that intermediate product to the final product.
4. If that was the most significant digit in the multiplier, then we are done.
5. Else we are not done, so shift the multiplicand left by one digit and select the next higher digit in the multiplier.
6. Goto to Step 2.
Binary multiplication is exactly the same. For example, consider the problem of 11×5 (1011B × 101B), wherein 1011B is the multiplicand and 101B is the multiplier:
Code:
```         1011B = 11D
× 101B =  5D
--------
1011B
0B
101100B
-------
110111B = 55D```
Again using the 1960's "new math", what's actually happening here is:
1011B×1B + 1011B×100B = 110111B
Note that since the zero digit is zero, that term has been left out. And performing a similar operation as I had above for the decimal problem, we get:
1011B×1B + 101100B×1B = 110111B
which, since anything times one is itself, is the same as:
1011B + 101100B = 110111B
or more specifically:
1011B + 1011B << 2 = 110111B

And since we're either multiplying by zero, which gives us a term of zero, or one, which gives us the current shifted form of the multiplicand, the procedure is simplified to:
1. Initialize the product to zero and start with the least significant bit of the multiplier.
2. If the bit is zero, go to Step 4.
3. Bit is one, so add the current shifted form of the multiplicand to the final product.
4. If that was the most significant bit in the multiplier, then we are done.
5. Else we are not done, so shift the multiplicand left by one bit and select the next higher bit in the multiplier.
6. Goto to Step 2.
That's the general procedure that will work for all possible binary multipliers. But since you know what the multiplier will always be (ie, ten-decimal), you could simplify the code by applying 1960's "new math" as above, though for 10-decimal instead of five.

PS

Another way to approach multiplying by ten in binary would be through this algebraic identity from the Distributive Law:
a(m+n) = am + an

So for (m+n) being ten, come up with a sum of powers of two that add up to ten, such as 8 + 2. You know how to use bit shifting to multiply by powers of two.
Last edited by dwise1_aol; February 21st, 2013 at 04:22 PM.