Skip to content

Instantly share code, notes, and snippets.

@fischerbach
Created August 30, 2023 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fischerbach/16a8d51eedf522c6e80615e39d4370e5 to your computer and use it in GitHub Desktop.
Save fischerbach/16a8d51eedf522c6e80615e39d4370e5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main() {
float f = 1234.5678f;
union {
float f;
unsigned char b[4];
} my_f;
my_f.f = f;
printf("float: %f\n", my_f.f);
printf("bytes: %.2x %.2x %.2x %.2x\n",
my_f.b[3], my_f.b[2], my_f.b[1], my_f.b[0]);
my_f.b[3] |= 0x80; //0x80 == binary 1000 0000
printf("float: %f\n", my_f.f); //should return negative value
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment