Multiply a given Integer with 3.5

来源:互联网 发布:淘宝50字通用好评 编辑:程序博客网 时间:2024/06/09 07:59

reference: 

http://www.geeksforgeeks.org/multiply-an-integer-with-3-5/


Problem Definition:

Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.

Examples:
Input: 2
Output: 7

Input: 5
Output: 17 (Ignore the digits after decimal point)



Solution:

This could be done by equation (8*x – x)/2 . 


Code:

int multiplyWith3Point5(int x){  return ((x<<3) - x)>>1;}    


原创粉丝点击