I'm reading Computer Systems: A Programmer’s Perspective, then I found the Special Values
's definition and corresponding bit patterns.
Now, I wanna output their bits using C++. I use their macro to output bits, obviously is incorrect, because macro defined to Integer!
#define FP_NAN 0x0100
#define FP_NORMAL 0x0400
#define FP_INFINITE (FP_NAN | FP_NORMAL)
What should I do to correctly output bits in the image above? and, Why compiler defined those Integer macros rather than IEEE standard?
below is my code.
#include <iostream>
#include <cmath>
#include <bitset>
using namespace std;
union U {
float f;
int i;
};
int main() {
U u1, u2;
u1.f = FP_NAN;
u2.f = FP_INFINITE;
cout << bitset<32>(u1.i) << endl;
cout << bitset<32>(u2.i) << endl;
return 0;
}
output:
01000011100000000000000000000000
01000100101000000000000000000000
My computer environment:
- win10
- mingw64
I wrote a quick-and-dirty
double
bit-wise output program a while back. You could modify it to work forfloat
.It has ANSI escape sequences in it, which might not be suitable for your environment.
The key part is just using a byte memory pointer and examining the bit state directly, rather than trying to get
std::bitset
to play nice.