Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void ieee754encode( float value, char * encoded );
- void ieee754encode(float value, char * encoded){
- int i, *p;
- p = &value;
- printf("input: %f\n", value);
- printf("sign: %d\n", (value >= 0.0) ? 0 : 1 );
- printf("exponent: ");
- //32 would be first bit (go right to left), -1 to account for 0, -1 to skip sign bit
- for(i = 30; i > 22; --i) printf("%d", (*p) >> i & 1 ); //print first 8 bits after sign
- printf("\nfraction: ");
- for(i = 22; i >= 0; --i) printf("%d", (*p) >> i & 1 ); //print the next 16 bits
- printf("\noutput: ");
- for(i = 31; i >= 0; --i) { //print all 32 bits now
- printf("%d", (*p) >> i & 1 );
- //throw shit onto string
- char tmp[1];
- sprintf(tmp, "%d", (*p) >> i & 1 );
- strcat(encoded, tmp);
- }
- printf("\n");
- }
- int main(){
- char * binary = calloc(sizeof(char), 33);
- float input = 57.75;
- ieee754encode(input, binary);
- //test string:
- int i;
- printf("testing string:\n");
- printf("output: ");
- for(i = 0; i < 32; ++i) printf("%c", binary[i]);
- printf("\n");
- free(binary);
- }
- //0100001001100111000000000000000
- //0100001001100111000000000000000
- // 000000000000000
- // 10000100
- // 11001110000000000000000
- // 11001110000000000000000
- //0100001001100111
- //01000010011001110000000000000000
- //01000010011001110000000000000000
- //01000010011001110000000000000000
- //0100001001100111000000000000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement