Domanda di colloquio di Google
Write a function with integer input which returns an integer having been flipped around in the binary sense. Example:
Given 3, it should be turned into 0000 0011, then flipped 1100 0000, then return 64+128 = 192. I commented that this was easily done via bitwise manipulations, but I have not done those in a while so I wrote a program out in C to do it manually.
Risposte di colloquio
public int flipInteger (int n)
{
int i, newInt = 0;
for (i = 0; i >= 1;
}
return newInt;
}
int main()
{
int num, total;
cout > num;
stack s;
int buffer[8], i=0;
int dec = 0;
while(num > 0)
{
total = num % 2;
num = num/2;
s.push(total);
}
while(s.size() != 0)
{
buffer[i] = s.top();
s.pop();
i++;
}
for(int j=i; j<8; j++)
buffer[j] = 0;
cout << "The converted number is ";
for(int a=0; a<8; a++)
cout << buffer[a];
cout << "\n";
cout << "The decimal version of it is ";
int count = 7;
for(int q=0; q<8; q++)
{
dec = dec + buffer[q]*(pow(2,count));
count--;
}
cout << dec;
getchar();
return 0;
}