I want to print the bits of a float number.
My plan was to:
- Discover how many bits there are at all
- Create and int array and insert into it the bits of the number using simple right bits shifting.
- Because the bits were inserted in a reversed order to what I need, lets run another loop to go over the array one again but this time, from the last element to the first one.
Why?
Lets take for example the number 4.2: I believe the bits were entered in the following way:
4 - 1000
2 - 0010
So together it's 10000010.
Enters FILO - First in -> Last out. So 0 will be the first element but as you cans see here, we need it in the end.
Here is my code:
float FloatAnalysis(float number)
{
int arr[32] = {0};
float num_cpy = number;
size_t num_of_bits = 0, i = 0;
while (0 != number)
{
num_cpy >>= 1;
++num_of_bits;
}
num_cpy = number;
for(i = 0; i < num_of_bits; ++i)
{
arr[i] = (num_cpy & 1);
num_cpy >>= 1;
}
for(i = num_of_bits-1; i => 0; --i)
{
printf("%d", arr[i]);
}
}
And here the output:
bitwise.c:359:11: error: invalid operands to binary >> (have ‘float’ and ‘int’)
359 | num_cpy >>= 1;
| ^~~
bitwise.c:368:21: error: invalid operands to binary & (have ‘float’ and ‘int’)
368 | arr[i] = (num_cpy & 1);
| ^
bitwise.c:369:11: error: invalid operands to binary >> (have ‘float’ and ‘int’)
369 | num_cpy >>= 1;
| ^~~
Can you expl
ain me what is going on here?