Advertisement
Guest User

Remember to trick MOSS before using

a guest
Oct 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void ieee754encode( float value, char * encoded );
  6.  
  7. void ieee754encode(float value, char * encoded){
  8. int i, *p;
  9. p = &value;
  10. printf("input: %f\n", value);
  11. printf("sign: %d\n", (value >= 0.0) ? 0 : 1 );
  12. printf("exponent: ");
  13. //32 would be first bit (go right to left), -1 to account for 0, -1 to skip sign bit
  14. for(i = 30; i > 22; --i) printf("%d", (*p) >> i & 1 ); //print first 8 bits after sign
  15. printf("\nfraction: ");
  16. for(i = 22; i >= 0; --i) printf("%d", (*p) >> i & 1 ); //print the next 16 bits
  17. printf("\noutput: ");
  18. for(i = 31; i >= 0; --i) { //print all 32 bits now
  19. printf("%d", (*p) >> i & 1 );
  20. //throw shit onto string
  21. char tmp[1];
  22. sprintf(tmp, "%d", (*p) >> i & 1 );
  23. strcat(encoded, tmp);
  24. }
  25. printf("\n");
  26. }
  27.  
  28. int main(){
  29. char * binary = calloc(sizeof(char), 33);
  30. float input = 57.75;
  31. ieee754encode(input, binary);
  32.  
  33. //test string:
  34. int i;
  35. printf("testing string:\n");
  36. printf("output: ");
  37. for(i = 0; i < 32; ++i) printf("%c", binary[i]);
  38.  
  39. printf("\n");
  40. free(binary);
  41. }
  42.  
  43. //0100001001100111000000000000000
  44. //0100001001100111000000000000000
  45. // 000000000000000
  46. // 10000100
  47. // 11001110000000000000000
  48. // 11001110000000000000000
  49. //0100001001100111
  50.  
  51. //01000010011001110000000000000000
  52. //01000010011001110000000000000000
  53.  
  54. //01000010011001110000000000000000
  55. //0100001001100111000000000000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement