In particular the above seems very pointless...Code:mantissa = mantissa << 9; mantissa = mantissa >> 9;
--
Mats
This is a discussion on how to convert decimal to floating point number within the C Programming forums, part of the General Programming Boards category; Code: mantissa = mantissa << 9; mantissa = mantissa >> 9; In particular the above seems very pointless... -- Mats...
In particular the above seems very pointless...Code:mantissa = mantissa << 9; mantissa = mantissa >> 9;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
This is also wrong. Why would you shift the exponent left when you want it at the beginning, to the right? If you remove the left shift, it's correct.Code:exponent = exponent << 1 exponent = exponent >> 23;
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
okay I fixed all the errors I have, now how do I translated the mantissa into hexadecimals. like for instance if it's -10 then it should be 0xa00000 in the mantissa
Last edited by -EquinoX-; 03-03-2008 at 12:20 PM.
Your bits are in the wrong position, that's why it seems you are getting "excess."
You can use my function for debugging:
It will print all bits of a number. Make sure there are no "0" to the right-most, because if there it, you didn't shift it right.Code:void PrintBits(uint32_t nNumber) { char Binary[sizeof(nNumber) * 8 + 1] = {0}; char Binary2[sizeof(nNumber) * 8 + 1] = {0}; for (int i = sizeof(nNumber) * 8 - 1; i >= 0; i--) { if (nNumber & (0x1 << i)) Binary[i] = '1'; else Binary[i] = '0'; } for (int i = sizeof(nNumber) * 8 - 1, j = 0; i >= 0; i--, j++) Binary2[j] = Binary[i]; for (int i = 0; i < sizeof(nNumber) * 8; i++) { if (i == 1) cout << " "; else if (i == 1 + 8) cout << " "; cout << Binary2[i]; } cout << "\n"; }
Shift it all the way to the right. And be careful not to shift too much.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I think what brewbuck referred to was if you extracted a number such as
1111 11111 0000 0000
Shift left 9 times and we get:
1000 0000 0000 0000
Shift right 9 times and we get:
0000 0001 0000 0000
Unintentionally destroyed data.
But it depends on the size. It it was 32-bit, obviously this wouldn't happen.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
yes I am doing this on a 32 bit machine
I mean the size of the variable. My example was of a 16-bit variable.
But it goes to show that you should be careful about shifting the original data since you can unintentionally destroy parts of it.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Well, as I mentioned, for the number -10.0, you should get:
Mantissa 0x00200000
Exponent 0x0082
Sign 0x01
If you don't, then there's a bug somewhere in the code. Look at the results you are getting and compare them to these. If they don't match, then they're wrong.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
as I said the hidden value, which is 1. x x x x x is included here
my other question is that say that I have an unsigned int 126 and I want to substract that with the int 127 so the result will be -1, how would I do this??
Last edited by -EquinoX-; 03-03-2008 at 04:24 PM.