Guest User

Untitled

a guest
Nov 18th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. int bit_return(int a, int loc)
  2.  
  3. // Bit returned at location
  4. {
  5. int buf = a & 1<<loc;
  6.  
  7. if (buf == 0)
  8. return 0;
  9. else
  10. return 1;
  11. }
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. int bit_return(int a, int loc) // Bit returned at location
  17. {
  18. int buf = a & 1<<loc;
  19. if (buf == 0)
  20. return 0;
  21. else
  22. return 1;
  23. }
  24.  
  25. int main()
  26. {
  27. int a = 289642; // Represent 'a' in binary
  28. int i = 0;
  29. for (i = 31; i>=0; i--)
  30. {
  31. printf("%d",bit_return(a,i));
  32. }
  33. return 0;
  34. }
  35.  
  36. #include <stdio.h>
  37.  
  38. // bit returned at location
  39. int bit_return(int a, int loc)
  40. {
  41. int buf = a & 1<<loc;
  42.  
  43. if (buf == 0) return 0;
  44. else return 1;
  45. }
  46.  
  47. int main()
  48. {
  49. //11000010111011010100000000000000
  50. // 1 sign bit | 8 exponent bit | 23 fraction bits
  51. float a = -118.625;
  52. int *b;
  53. b = &a;
  54.  
  55. int i;
  56. for (i = 31; i >= 0; i--)
  57. {
  58. printf("%d",bit_return(*b,i));
  59. }
  60.  
  61. return 0;
  62. }
  63.  
  64. static void printme(void *c, size_t n)
  65. {
  66. unsigned char *t = c;
  67. if (c == NULL)
  68. return;
  69. while (n > 0) {
  70. --n;
  71. printf("%02x", t[n]);
  72. }
  73. printf("n");
  74. }
  75.  
  76. void fpp(float f, double d)
  77. {
  78. printme(&f, sizeof f);
  79. printme(&d, sizeof d);
  80. }
  81.  
  82. while (n > 0) {
  83. int q;
  84. --n;
  85. for(q = 0x80; q; q >>= 1)
  86. printf("%x", !!(t[n] & q));
  87. }
  88.  
  89. #include <stdio.h>
  90. #include <stdlib.h>
  91.  
  92. void output_binary_fp_number(double arg)
  93. {
  94. double pow2;
  95.  
  96. if ( arg < 0 ) { putchar('-'); arg = -arg; }
  97. if ( arg - arg != 0 ) {
  98. printf("Inf");
  99. }
  100. else {
  101. /* compare and subtract descending powers of two, printing a binary digit for each */
  102. /* first figure out where to start */
  103. for ( pow2 = 1; pow2 * 2 <= arg; pow2 *= 2 ) ;
  104. while ( arg != 0 || pow2 >= 1 ) {
  105. if ( pow2 == .5 ) putchar('.');
  106. if ( arg < pow2 ) putchar('0');
  107. else {
  108. putchar('1');
  109. arg -= pow2;
  110. }
  111. pow2 *= .5;
  112. }
  113. }
  114.  
  115. putchar('n');
  116.  
  117. return;
  118. }
  119.  
  120. void usage(char *progname) {
  121. fprintf(stderr, "Usage: %s real-numbern", progname);
  122. exit(EXIT_FAILURE);
  123. }
  124.  
  125. int main(int argc, char **argv) {
  126. double arg;
  127. char *endp;
  128.  
  129. if ( argc != 2 ) usage(argv[0]);
  130. arg = strtod(argv[1], &endp);
  131. if ( endp == argv[1] || *endp ) usage(argv[0]);
  132.  
  133. output_binary_fp_number(arg);
  134.  
  135. return EXIT_SUCCESS;
  136. }
  137.  
  138. float f = 42.69;
  139.  
  140. for ....
  141. bit_return((int) f, loc)
  142.  
  143. bit_return (*((float *) &f), loc)
  144.  
  145. float myfloat = 254940.4394f;
  146. printf("0x%p", *(void**)(&myfloat));
  147.  
  148. float f = ...
  149. int int_part = floor(f)
  150. int fraction_part = floor((f - int_part) * pow(2.0, 32))
  151.  
  152. float f = ...
  153. int int_part = floor(f)
  154. int fraction_part = floor((f - int_part) * pow(2.0, 32))
Add Comment
Please, Sign In to add comment