Last active
June 29, 2022 05:26
-
-
Save mralfarrakhan/bde292b1a64a158303ea7d8929e05e99 to your computer and use it in GitHub Desktop.
julia inspired bitstring function
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 <iostream> | |
| #include <string> | |
| #include <cmath> | |
| #include <cassert> | |
| using str = std::string; | |
| static str itob(const unsigned int& num) { | |
| str res = ""; | |
| for(int i = 8 * sizeof(float) - 1; i >= 0; i--) { | |
| res += std::to_string((static_cast<unsigned int>(pow(2, i)) & num) != 0?1:0); | |
| } | |
| return res; | |
| } | |
| template <typename F> | |
| str bitstringt(F num, char delimiter = ' ') { | |
| static_assert(std::is_floating_point_v<F>, "num should be floating point types"); | |
| int nint = sizeof(num) / sizeof(int); | |
| unsigned int* vint = new unsigned int[nint]; //int array to cast | |
| vint = reinterpret_cast<unsigned int*>(&num); | |
| str res = ""; | |
| for(int i = nint - 1; i >= 0; i--) { | |
| res += itob(vint[i]) + delimiter; | |
| } | |
| return res; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment