F
frank
Elsethread there is a discussion of how to check whether a bit is set.
I'm trying to do something similar. So far it looks like this:
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra bits1.c -o out
dan@dan-desktop:~/source$ ./out
value is 0
count is 0
dan@dan-desktop:~/source$ cat bits1.c
#include <stdio.h>
#define VALUE_BITS 32
int main(void)
{
int count = 0;
int bit;
float x = .5;
int value;
value = (int) x;
printf("value is %d\n", value);
for(bit = 0; bit < VALUE_BITS; bit++)
{
if (value & 1)
count ++;
value >>= 1;
}
printf("count is %d\n", count);
return 0;
}
// gcc -std=c99 -Wall -Wextra bits1.c -o out
dan@dan-desktop:~/source$
The idea is that I want to see the bit pattern for an arbitrary float.
The above approach is what Lawrence Kirby starts out with in chp 5 of
Unleashed. (Presumably, his works.)
I've also received the following advice on how to do this:
C casts do the appropriate conversion. If you want to see the
bits through casts you have to dereference a pointer cast to
a pointer to a different type, though in that case the standard
does not guarantee the result.
To do it following the standard (at least C90), you cast a
pointer to (unsigned char *), memcpy() the bits to a variable
through a pointer also cast to (unsigned char *).
end quote
I don't really understand the shift operators, and they seem to be all we
have to do this.
Anyways, my question is how to look at the bit representation of a float
in C. Thanks for your comment and cheers,
I'm trying to do something similar. So far it looks like this:
dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra bits1.c -o out
dan@dan-desktop:~/source$ ./out
value is 0
count is 0
dan@dan-desktop:~/source$ cat bits1.c
#include <stdio.h>
#define VALUE_BITS 32
int main(void)
{
int count = 0;
int bit;
float x = .5;
int value;
value = (int) x;
printf("value is %d\n", value);
for(bit = 0; bit < VALUE_BITS; bit++)
{
if (value & 1)
count ++;
value >>= 1;
}
printf("count is %d\n", count);
return 0;
}
// gcc -std=c99 -Wall -Wextra bits1.c -o out
dan@dan-desktop:~/source$
The idea is that I want to see the bit pattern for an arbitrary float.
The above approach is what Lawrence Kirby starts out with in chp 5 of
Unleashed. (Presumably, his works.)
I've also received the following advice on how to do this:
C casts do the appropriate conversion. If you want to see the
bits through casts you have to dereference a pointer cast to
a pointer to a different type, though in that case the standard
does not guarantee the result.
To do it following the standard (at least C90), you cast a
pointer to (unsigned char *), memcpy() the bits to a variable
through a pointer also cast to (unsigned char *).
end quote
I don't really understand the shift operators, and they seem to be all we
have to do this.
Anyways, my question is how to look at the bit representation of a float
in C. Thanks for your comment and cheers,