Give a fast way to multiply a number by 7
Answers were Sorted based on User's Feedback
Answer / anu
Both answers have slight mistakes in them.
x *7 = ( x<<3 ) - x , which is equivalent to (x* 8 ) -x
| Is This Answer Correct ? | 65 Yes | 6 No |
Answer / rajan
ANU is right the answer is x<<3 -x
when you left shift any number by 1 it is equivalent to
multiplying with 2.
so, left shift by 3 means ur are multiplying with 8.
and x*7 = x(8-1) = x*8-x = x<<3-x .
| Is This Answer Correct ? | 31 Yes | 5 No |
Answer / anonymous
The easiest way is to relate the number to be multiplied
by, to a power of 2. Then a corresponding number of left
shifts to that power with an addition or subtraction would
give the desired result.
In this case, 7 can be written as 2^3 - 1 or 8 - 1.
Therefore, shift the number to the left thrice which would
multiply it by 8. Then subtract the original. You can also
do this as
4 + 2 + 1 = 2^2 + 2^1 + 1
| Is This Answer Correct ? | 20 Yes | 6 No |
Answer / pratik chopra
Yes this can be made generic;
Approach: 7*x=(8-1)*x= 8*x-1*x=x<<3-x
If 6*x=(4+2)x=4*x+2*x=x<<2+x<<1
If 13*x=(16-2-1)x=(x<<4-x<<1-x)
| Is This Answer Correct ? | 8 Yes | 0 No |
Answer / pratik chopra
It is very obvious above condition will not work if output
after multiplying by 7 is not 8 bit. In that case, if you
consider 64*7=448 which is 111000000 and a 9bit number.
Thus, there is an overflow. Even a normal decimal
calculation, restricted to 8 bit will not give a right answer.
In other words, any n-bit number whose multiplication with 7
is an n-bit number, the above solution will work otherwise,
overflow will occur.
For 8 bit no. max x<=(255/7)<=36.
| Is This Answer Correct ? | 7 Yes | 1 No |
Answer / pathfinder
it shall be
n*7 = (n << 3) - n as already have been suggested by some ppl.
| Is This Answer Correct ? | 7 Yes | 3 No |
Answer / rupesh
There is a problem with
x=(x<<3)-x;
Suppose that x is stored in 8 bit format.
If x = 64, i.e. 2^6, then this won't work.
In general, if x is n-bit, and value of x > 2^(n-3), then
this won't work.
| Is This Answer Correct ? | 4 Yes | 2 No |
In the following control structure which is faster? 1.Switch 2.If-else and which consumes more memory?
what is c language.
What is the use of typedef in c?
What is null pointer in c?
main() { int a=4,b=2; a=b<<a + b>>2; printf("%d", a); }
11 Answers HCL, Vector, Vector India, Vector Solutions, Wipro,
What would happen to X in this expression: X += 15; (assuming the value of X is 5)
what are the static variables
8 Answers HCL, iFlex, TCS, Wipro,
write a program to remove occurrences the word from entered text?
18)struct base {int a,b; base(); int virtual function1(); } struct derv1:base{ int b,c,d; derv1() int virtual function1(); } struct derv2 : base {int a,e; } base::base() { a=2;b=3; } derv1::derv1(){ b=5; c=10;d=11;} base::function1() {return(100); } derv1::function1() { return(200); } main() base ba; derv1 d1,d2; printf("%d %d",d1.a,d1.b) o/p is a)a=2;b=3; b)a=3; b=2; c)a=5; b=10; d)none 19) for the above program answer the following q's main() base da; derv1 d1; derv2 d2; printf("%d %d %d",da.function1(),d1.function1(),d2.function1 ()); o/p is a)100,200,200; b)200,100,200; c)200,200,100; d)none 20)struct { int x; int y; }abc; you can not access x by the following 1)abc-->x; 2)abc[0]-->x; abc.x; (abc)-->x; a)1,2,3 b)2&3 c)1&2 d)1,3,4
main() { printf(5+"Vidyarthi Computers"); }
What is true about the following C Functions (a) Need not return any value (b) Should always return an integer (c) Should always return a float (d) Should always return more than one value
difference between Low, Middle, High Level languages in c ?