Adobe Interview Question
Country: United States
I like Vicks solution better, it uses only use three operations (instead of 4):
return ((n>>6)+1)<<6;
@anuj
lets understand by simple example:
suppose 90 is the number after which we have to find multiple of 64
than binary of 90 is 01011010 and 90+64=154 whose binary is 10011010
now you can see after adding 64 to 90 the next bit(8th bit is set)
when you '&' it with `~63' i.e. 11000000 it gives u value 10000000 which is 128. so the trick is to add 64 to the value so that its next bit is set. which will be remain during logical ANDING of negation of 63.
your solution is right,
but the question says "with bit operation " so the former solution is more appropriate .
Use the following code to get the answer.
#include <stdio.h>
#include <math.h>
int main ()
{
float param, result;
scanf("%f", ¶m);
int leftmostsetbit = (int)log2(param);
int answer = 1;
answer = answer << (leftmostsetbit+1);
if(answer < 64)
printf("64\n");
else
printf("%d\n", answer);
getch();
return 0;
}
@aasshishh the question is "next multiple of 64 that follows it" so it should be 64,128,192,256...
but for your program, if I enter 130, I am getting 256 as output...!!!
please correct me if my understanding is wrong...
#include<iostream>
- Dev March 22, 2013using namespace std;
int main()
{
int a;
cout<<"enter a positive number..."<<endl;
cin>>a;
cout<< (a+64 &(~63));
return 0;
}