0

I am working on a project of converting any real number into binary using IEEE 754 format.
My first trial is using the bitset library type for conversion of the number then i can worry about dividing the whole number into sign bit, exponent and mantissa.

int foo;
cin >> foo;

bitset<32> my_bit(foo);

As it turns out, bitset will do with signed integers only.
How do i include floating point numbers?
Can i accomplish my task with another library type that is as fairly simple as bitset?

Samuel
  • 612
  • 2
  • 9
  • 25
  • @Jacob Then what's with `union` and array usage? – Samuel Mar 27 '15 at 18:10
  • Using a union as per link is the easiest way. It's not guaranteed to work by the standard but it can be acceptable to declare your code compatible only with certain compilers in the real world. – Neil Kirk Mar 27 '15 at 18:16
  • 1
    what about a dirty `*(uint32_t *)&` ? – user3528438 Mar 27 '15 at 18:21
  • @user3528438 what's that? i have no idea – Samuel Mar 27 '15 at 18:28
  • @samuel use `&` to take the address of whatever and cast it to `uint32_t *` load an `uint32_t` from there. – user3528438 Mar 27 '15 at 18:30
  • @samuel: that is a way of casting 32-bit `float` into the standard 32-bit integer type `uint32_t`, maintaining the binary representation intact. Presumably, you can then create `bitset` easily from `uint32_t`. – Violet Giraffe Mar 27 '15 at 18:45
  • @user3528438: That should certainly work, though technically that's not compliant either and a compiler making evil aliasing inferences may screw you over, and so `memcpy` would be preferred. Admittedly the internal floating-point representation isn't guaranteed either so going whole hog would be to assemble an IEEE-754 bitfield via `frexp`. – doynax Mar 27 '15 at 19:09

1 Answers1

2

Actually, bitset constructor accepts unsigned long in C++ 03 and unsigned long long in C++ 11. Now, as for storing float in a bitset, this should do the trick:

float f = 0.0f;
cin >> f;
bitset<32> my_bit(*(uint32_t*)&f); // my_bit? What kind of a name is that, anyway?..
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • 1
    I'd rather do `*(uint32_t *)&` first then (implicitly) convert it to `unsigned long`, considering `long` is longer than `float` on a lot of machines now. – user3528438 Mar 27 '15 at 18:56
  • I think you are missing a `*`: `bitset<32> my_bit(*(unsigned long*)&f);` – TonyK Mar 27 '15 at 19:00
  • @Violet Giraffe, it's not working. *there is an invalid type argument of unary *" – Samuel Mar 27 '15 at 19:01
  • it's now working thanks to @TonyK but guys i need help understanding the logic behind it because I AM NOT AND I NEED TO. – Samuel Mar 27 '15 at 19:03
  • @user3528438: agreed – Violet Giraffe Mar 27 '15 at 19:09
  • @TonyK: fixed, good catch. – Violet Giraffe Mar 27 '15 at 19:09
  • @samuel: You're basically asking about C++ syntax now, which is sort of outside of the scope of the question as it's worded now. Don't get me wrong, but explaining it would take a fair amount of typing and I'm too tired after a long day at work to do it. Perhaps, someone else will expand my answer by editing, or post their own, more elaborate answer. – Violet Giraffe Mar 27 '15 at 19:13
  • @VioletGiraffe Thanks though! please whoever is going to i need a little help with the usage of `(*(uint32_t*)&f)`. – Samuel Mar 27 '15 at 19:16
  • 1
    @samuel It's not that hard. `&f` takes the address where `f` is stored, then `(uint32_t *)` makes compiler think that address of an `uint_32_t`, and when `*` dereferences it, it present the raw binary data in that memory address as if it were `uint32_t`. This is the C way of reinterpret_cast. – user3528438 Mar 27 '15 at 20:20
  • @user3528438 thank you very much, i am still a beginner this is why i am having a hard time understanding concept that might seem obvious to an experienced programmer. – Samuel Mar 27 '15 at 20:25
  • @user3528438 just one more thing, what is the difference between `uint32_t` and `unsigned int`? – Samuel Mar 27 '15 at 20:48
  • @samuel: usually there is none, but the size of `int` (so `unsigned int`, too) is not specifically set by the standard, while `unit32_t` is 32 bit by definition. `int` most often is 32 bit as well, but it's not guaranteed to be. – Violet Giraffe Mar 27 '15 at 20:59