Home c++ Is it possible to double "shove" in Bitset so that he brought...

# Is it possible to double “shove” in Bitset so that he brought it in 2Vide?

Author

Date

Category

Is it possible to double “shove” in Bitset so that he brought it in 2wide?

wrote code, something is not very good:

``````double number = 123.456;
Unsigned long Long Bits = * Reinterpret_cast & lt; unsigned long long * & gt; (& amp; number);
COUT & LT; & LT; "Double:" & lt; & lt; BITS & LT; & LT; Endl;
COUT & LT; & LT; "Size Double:" & lt; & lt; Sizeof (Bits) & lt; & lt; Endl;
BITSET & LT; Sizeof (BITS) & GT; BITSET_NUMBER {BITS};
COUT & LT; & LT; "Binary:" & lt; & lt; BITSET_NUMBER & LT; & LT; Endl;
COUT & LT; & LT; "Dec:" & lt; & lt; BITSET_NUMBER.TO_ULONG () & lt; & lt; Endl;
``````

I think I want somewhere this

``````double a = 1.5;
AUTO B = STD :: BITSET & LT; 64 & gt; (* (Unsigned long Long *) (& amp; a));
STD :: COUT & LT; & LT; B & LT; & lt; '\ n';
// Output 0011111111111111110000000000000000000000000000000000.
B.set (63); // Give the iconic bit, it should work out -1.5
STD :: COUT & LT; & LT; B & LT; & lt; '\ n';
// Conclusion 1011111111111111110000000000000000000000000000000000.
unsigned long long c = b.to_ullong ();
STD :: COUT & LT; & LT; (* (Double *) (& amp; c)) & lt; & lt; '\ n';
// Conclusion -1.5
``````

play yourself and make sure you can https://evanw.github.io/float-toy /

Update

I introduced 12.15, and it gives no way.

It all gives it, just need to “see.” At 12.15 there will be an output

``````01000000001010010011111111001111111111111111111110101
+ Exhibit. ^ It begins the number (mantis).
``````

is not yet visible? Close your eyes to the exhibitor (these are the first 12 characters), we get 10000100110011001111100111111001111101001111101.

then We look into the number of numbers and understand that since any no zero number will always contain 1 at the very beginning, then there is actually recorded
110000100110011001100110011111001100111110011001101

Compare it from 1100.00100110011 (and do not forget that here in fact it is necessary to write so 1100.00 (1001) – this is a periodic fraction!), We see that this one in one.

It remains to understand where the point to put. And in this we will help the exhibitor. We have 100,000,00010. The leftmost single bit is a sign, but on the contrary. In this case, this is a plus. And it turns out the degree 2 in 4. That is, the point must be put after 4 characters in the record. With negative degrees there is more complicated, but also solved.

UPD

If this code has decided to send the sale, then of course it is impossible to write due to the fact that modern compilers such castes are not very welcome. In this case, it is better to write somewhere so

``````double a = 1.5;
unsigned long Long BX = 0;
// Consume sure that we have the same size.
Static_assert (Sizeof (a) == SizeOf (BX), "Sizeof (Double) == SizeOF (ULL)");
// Copy. Smart compilers can make this code no worse than a caste
STD :: MEMCPY (& AMP; BX, & AMP; A, SizeOF (BX));
// and then already according to the classics
AUTO B = STD :: BITSET & LT; 64 & GT; (BX);
``````

To start: All your code can be replaced by `COUT & LT; & LT; STD :: BITSET & LT; 64 & GT; (Number); `and will be taken a whole part of the number. Now let’s try to figure out how to get the whole number.

Floating point numbers have two parts (a whole and fractional part). To be able to represent them as one part, you need to specify the exhibitor (order of number). Then you can move the floating point to the right to the specified order, presented in binary form, and then move the point to the left to the same. Sample code:

``````const doubles number = 123.456,
EXP = 3,
E = POW (10, EXP);
STD :: BITSET & LT; 64 & gt; b (Number * E);
unsigned long n = b.to_ullong ();
COUT & LT; & LT; B & LT; & lt; '\ n'
& lt; & lt; n & lt; & lt; "E-" & lt; & lt; EXP;
// Get a floating point number
COUT & LT; & LT; "\ Noriginal Number:" & lt; & lt; (Double) N / E;
`````` 0100000000101000010011001100111111111111110011111111110110011001101

+ Exhibitor __ ^ Mantissa Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.