How to double a value without using * and + operators in C

I need a logic to double a given no without using + and * operators

Page 1 of 1

1 Replies - 1328 Views - Last Post: 23 June 2009 - 12:39 PM Rate Topic: -----

#1 johnmcjoel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-June 09

How to double a value without using * and + operators in C

Post icon  Posted 23 June 2009 - 12:09 PM

I need a logic to double a given no without using + and * operators
Is This A Good Question/Topic? 0
  • +

Replies To: How to double a value without using * and + operators in C

#2 Tiron  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 26
  • Joined: 03-October 08

Re: How to double a value without using * and + operators in C

Posted 23 June 2009 - 12:39 PM

If it is an int/char, using a left bitshift would double the value, and using a right bitshift would half it.

int i = 50;
i = i << 1; // i * 2


If you shift by 2
i << 2
you would be multiplying by 4 (2 * 2 = 0), and shifting 3 bits would be 8;

This post has been edited by Tiron: 23 June 2009 - 12:40 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1