In particular the above seems very pointless...Code:mantissa = mantissa << 9;
mantissa = mantissa >> 9;
--
Mats
Printable View
In particular the above seems very pointless...Code:mantissa = mantissa << 9;
mantissa = mantissa >> 9;
--
Mats
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;
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
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.
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.
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.
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.
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??