Skip to content

Instantly share code, notes, and snippets.

@hz9xa
Created August 15, 2013 01:25
Show Gist options
  • Save hz9xa/6237443 to your computer and use it in GitHub Desktop.
Save hz9xa/6237443 to your computer and use it in GitHub Desktop.
Floating Point to Binary Value(C++)
#include <iostream>
#include <bitset>
int main()
{
union
{
float input; // assumes sizeof(float) == sizeof(int)
int output;
} data;
data.input = 2.25125;
std::bitset<sizeof(float) * CHAR_BIT> bits(data.output);
std::cout << bits << std::endl;
// or
std::cout << "BIT 4: " << bits[4] << std::endl;
std::cout << "BIT 7: " << bits[7] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment