Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int bit_return(int a, int loc)
- // Bit returned at location
- {
- int buf = a & 1<<loc;
- if (buf == 0)
- return 0;
- else
- return 1;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int bit_return(int a, int loc) // Bit returned at location
- {
- int buf = a & 1<<loc;
- if (buf == 0)
- return 0;
- else
- return 1;
- }
- int main()
- {
- int a = 289642; // Represent 'a' in binary
- int i = 0;
- for (i = 31; i>=0; i--)
- {
- printf("%d",bit_return(a,i));
- }
- return 0;
- }
- #include <stdio.h>
- // bit returned at location
- int bit_return(int a, int loc)
- {
- int buf = a & 1<<loc;
- if (buf == 0) return 0;
- else return 1;
- }
- int main()
- {
- //11000010111011010100000000000000
- // 1 sign bit | 8 exponent bit | 23 fraction bits
- float a = -118.625;
- int *b;
- b = &a;
- int i;
- for (i = 31; i >= 0; i--)
- {
- printf("%d",bit_return(*b,i));
- }
- return 0;
- }
- static void printme(void *c, size_t n)
- {
- unsigned char *t = c;
- if (c == NULL)
- return;
- while (n > 0) {
- --n;
- printf("%02x", t[n]);
- }
- printf("n");
- }
- void fpp(float f, double d)
- {
- printme(&f, sizeof f);
- printme(&d, sizeof d);
- }
- while (n > 0) {
- int q;
- --n;
- for(q = 0x80; q; q >>= 1)
- printf("%x", !!(t[n] & q));
- }
- #include <stdio.h>
- #include <stdlib.h>
- void output_binary_fp_number(double arg)
- {
- double pow2;
- if ( arg < 0 ) { putchar('-'); arg = -arg; }
- if ( arg - arg != 0 ) {
- printf("Inf");
- }
- else {
- /* compare and subtract descending powers of two, printing a binary digit for each */
- /* first figure out where to start */
- for ( pow2 = 1; pow2 * 2 <= arg; pow2 *= 2 ) ;
- while ( arg != 0 || pow2 >= 1 ) {
- if ( pow2 == .5 ) putchar('.');
- if ( arg < pow2 ) putchar('0');
- else {
- putchar('1');
- arg -= pow2;
- }
- pow2 *= .5;
- }
- }
- putchar('n');
- return;
- }
- void usage(char *progname) {
- fprintf(stderr, "Usage: %s real-numbern", progname);
- exit(EXIT_FAILURE);
- }
- int main(int argc, char **argv) {
- double arg;
- char *endp;
- if ( argc != 2 ) usage(argv[0]);
- arg = strtod(argv[1], &endp);
- if ( endp == argv[1] || *endp ) usage(argv[0]);
- output_binary_fp_number(arg);
- return EXIT_SUCCESS;
- }
- float f = 42.69;
- for ....
- bit_return((int) f, loc)
- bit_return (*((float *) &f), loc)
- float myfloat = 254940.4394f;
- printf("0x%p", *(void**)(&myfloat));
- float f = ...
- int int_part = floor(f)
- int fraction_part = floor((f - int_part) * pow(2.0, 32))
- float f = ...
- int int_part = floor(f)
- int fraction_part = floor((f - int_part) * pow(2.0, 32))
Add Comment
Please, Sign In to add comment