Manipulating individual bits in a double precision number
I'm trying to display double precision numbers in binary form so that I can manipulate them on the bit level. I'm pretty sure I've figured out how to display them in binary form, only I'm getting weird results that I can't make sense of.
I have the following code:
Code:
#include <bitset>
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main(){
cout << "1: \n"
<< bitset<numeric_limits<unsigned long>::digits>(1)
<< endl;
cout << "-1: \n"
<< bitset<numeric_limits<unsigned long>::digits>((-1))
<< endl;
cout << "5: \n"
<< bitset<numeric_limits<unsigned long>::digits>(5)
<< endl;
cout << "-5: \n"
<< bitset<numeric_limits<unsigned long>::digits>(-5)
<< endl;
cout << "4503599627370496: \n"
<< bitset<numeric_limits<unsigned long>::digits>(4503599627370496)
<< endl;
cout << "-4503599627370496: \n"
<< bitset<numeric_limits<unsigned long>::digits>(-4503599627370496)
<< endl;
cout << "4503599627370496e1: \n"
<< bitset<numeric_limits<unsigned long>::digits>(4503599627370496e1)
<< endl;
cout << "4503599627370496e-1: \n"
<< bitset<numeric_limits<unsigned long>::digits>(4503599627370496e-1)
<< endl;
return 0;
}
which results in:
Code:
1:
0000000000000000000000000000000000000000000000000000000000000001
-1:
1111111111111111111111111111111111111111111111111111111111111111
5:
0000000000000000000000000000000000000000000000000000000000000101
-5:
1111111111111111111111111111111111111111111111111111111111111011
4503599627370496:
0000000000010000000000000000000000000000000000000000000000000000
-4503599627370496:
1111111111110000000000000000000000000000000000000000000000000000
4503599627370496e1:
0000000010100000000000000000000000000000000000000000000000000000
4503599627370496e-1:
0000000000000001100110011001100110011001100110011001100110011001
My understanding is that a double precision number is suppose to have the following layout:
1 sign bit, 11 exponent bits, 52 fraction bits
If this is true, then there must be something wrong with my code because "1" and "-1" should only have one bit that is different. What is wrong with my code?