Created
April 2, 2016 15:34
-
-
Save JPalounek/bdadc86a2edcd4e101422a409ac9a526 to your computer and use it in GitHub Desktop.
IEEE754 single precision float memory read
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
void lw(char *addr, char *target, int size) { | |
unsigned char bit; | |
for (int i = 0; i < size * 8; ++i) { | |
bit = *((unsigned char*) addr + ((int) i / 8)); | |
// Endianity - LE - size * 8 - i | |
target[size * 8 - i - 1] = bit >> i % 8 & 1; | |
} | |
} | |
void pr_f(char *bits) { | |
printf("IEEE754: "); | |
for (int i = 0; i < 32; i++) { | |
if(i == 1 || i == 9) { | |
printf(" "); | |
} | |
printf("%d", bits[i]); | |
} | |
printf("\n"); | |
} | |
void print_i3e(float a) { | |
char *a_addr = (void*)&a; | |
char a_bits[4 * 8]; | |
lw((void*)&a, a_bits, 4); | |
pr_f(a_bits); | |
} | |
int main() { | |
print_i3e(1.000505e003); | |
print_i3e(1.127500e000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment