thoughtful me
thoughtful me

Reputation: 115

How to multiply a number with 3.5 without using any standard operator like *,-,/,% etc?

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;
}

Upvotes: 0

Views: 723

Answers (4)

dumbak
dumbak

Reputation: 404

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;

Upvotes: 0

Paul R
Paul R

Reputation: 212949

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

#include <cmath>

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

LIVE DEMO

Upvotes: 1

Jarod42
Jarod42

Reputation: 217145

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 +

Upvotes: 1

Pascal Roessner
Pascal Roessner

Reputation: 111

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

Upvotes: 1

Related Questions