0

The Question is pretty straight forward.I am given a number and I want to multiply it with 3.5 i.e to make number n=3.5n .I am not allowed to use any operator like +,-,*,/,% etc.But I can use Bitwise operators.

I have tried by myself but It is not giving precise result like my program gives output 17 for 5* 3.5 which is clearly wrong.How can I modify my program to show correct result.

#include<bits/stdc++.h>
using namespace std;

double Multiply(int n)
{
   double ans=((n>>1)+ n + (n<<1));
   return ans;
}
int main()
{

  int n;            // Enter the number you want to multiply with 3.5
  cin>>n;
  double ans=Multiply(n);
  cout<<ans<<"\n";
  return 0;
}
  • 1
    Even if your function worked it uses `+` which you say is not allowed as part of this assignment? – Paul R Jan 19 '16 at 08:13
  • Oh yes.Maybe you can tell me how to do it then. – thoughtful me Jan 19 '16 at 08:16
  • Maybe people who are downvoting would like to tell me what is wrong about my question,so that I can improve :) – thoughtful me Jan 19 '16 at 08:17
  • 2
    Are you SURE the task is meant to do this for floating point values? That's really not easy - at least not for arbitrary ranges of float (e.g. 0.001 * 3.5 as well as 1E30 * 3.5), although that is entirely possible to do using simple logic, since that is all the microprocessor gets to use ever. – Mats Petersson Jan 19 '16 at 08:22
  • If the answer is for integer values, then it can be solved reasonably well using bitwise operations. – Mats Petersson Jan 19 '16 at 08:24
  • Sorry not to mention that number given will be integer – thoughtful me Jan 19 '16 at 08:25
  • But you say you want an exact result, e.g. `5 * 3.5 = 17.5`, so the result can not be integer ? – Paul R Jan 19 '16 at 08:27
  • I want exact 17.5 but number given will be Integer like we are given '5' in this case.Sorry If i created any confusion – thoughtful me Jan 19 '16 at 08:35
  • Right, so you'd have to calculate the result as twice the value, by "multiplying by 7" and then checking the lowest bit, and then print the `.5` or `.0` "by hand". So 5 * 7 = 35, remember that it is odd and shift the number `>> 1`, which gives 17, then print `".5"` as a string. – Mats Petersson Jan 19 '16 at 08:40

4 Answers4

1

One solution would be to use fma() from <cmath>:

#include <cmath>

double Multiply(int n)
{
    return fma(x, 3.5, 0.0);
}

LIVE DEMO

Paul R
  • 208,748
  • 37
  • 389
  • 560
1

Sorry I cannot comment yet. The problem with your question is that bitwise operations are usually only done on ints. This is mainly because of the way that numbers are stored.

When you have a normal int, you have a sign bit followed by data bits, pretty simple and straight forward but once you get to floating point numbers that simple patern is different. Here is a good explanation stackoverflow.

Also, the way I would solve your problem without using +/-/*// and so on would be

#include <stdlib.h> /* atoi() */
#include <stdio.h>  /* (f)printf */
#include <assert.h> /* assert() */

int add(int x, int y) {
    int carry = 0;
    int result = 0;
    int i;

    for(i = 0; i < 32; ++i) {
        int a = (x >> i) & 1;
        int b = (y >> i) & 1;
        result |= ((a ^ b) ^ carry) << i;
        carry = (a & b) | (b & carry) | (carry & a);
    }

    return result;
}

int negate(int x) {
    return add(~x, 1);
}

int subtract(int x, int y) {
    return add(x, negate(y));
}

int is_even(int n) {
    return !(n & 1);
}

int divide_by_two(int n) {
    return n >> 1;
}

int multiply_by_two(int n) {
   return n << 1;
}

Source

Community
  • 1
  • 1
1

From your solution, you may handle odd numbers manually:

double Multiply(unsigned int n)
{
   double = n + (n << 1) + (n >> 1) + ((n & 1) ? 0.5 : 0.);
   return ans;
}

but it still use +

Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Simply.

First realize that 3.5 = 112 / 32 = (128 - 16) / 32.

Than you do:

int x128 = ur_num << 7;
int x16 = ur_num << 4;

to subtract them use:

int add(int x, int y) {
int carry = 0;
int result = 0;
int i;

for(i = 0; i < 32; ++i) {
    int a = (x >> i) & 1;
    int b = (y >> i) & 1;
    result |= ((a ^ b) ^ carry) << i;
    carry = (a & b) | (b & carry) | (carry & a);
}
return result;
}

int negate(int x) {
return add(~x, 1);
}

int subtract(int x, int y) {
return add(x, negate(y));
}

and than just simply do:

int your_res = subtract(x128, x16) >> 5;
dumbak
  • 404
  • 3
  • 8