Adobe Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Simplest to me seems like breaking a float number into two parts. One part being its integer part, represented as an integer number and second part being the decimal part represented as an integer number as well. One you have that then just count the number of bits set in each integer. For example:
float x = 16.15f;
int int_x = (int) x;
int dec_x = (int) ((x - int_x) * 10000);
int_x = 16 and dec_x = 1499
There's basically something like an assembly instruction that can move the bits from a floating-point register to an integer register in the CPU bit-for-bit. In Java, there's a function called Float.floatToRawIntBits that I think generally just accesses that assembly instruction. You would then be free to use bit operations on that int.
public int countFloatBits (float value)
{
int intVal = Float.floatToRawIntBits(intVal);
int numBitsSet = 0;
while (intVal != 0)
{
intVal = intVal << 1;
numBitsSet++;
}
return numBitsSet;
}
At least I think that this is the kind of thing this question is asking for...
The i guess is that first convert the given floating number to binary. The conversion has a procedure which is done by multiplying the fractional part by 2. I dont remember exactly but can be found on net. Then, simply count the set bits.
- Nascent April 11, 2012