so say that I have an unsigned integer :
unsigned int exponent = 126;
exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead
so say that I have an unsigned integer :
unsigned int exponent = 126;
exponent = exponent - 127 will result -1?? I tried this and it gives me a really big number instead
Don't quote me on that... ...seriously
okay and say that I want to convert the unsigned int to an int can I do that so that I will be able to do this?
Yes. You can convert an unsigned int to signed. In fact, the operations (add, subtraction) are the same for unsigned and signed. So the result you get as far as the actual binary data stored in memory from the operation 0 - 1 on signed integers is the same as you would get with unsigned.
If you looked at the binary memory for both i and u, they would look identical. So the conversion from unsigned to sign is really just a matter of how the program interprets the memory.Code:unsigned int u = 0 - 5; int i = 0 - 5;
i would now hold the value -5Code:unsigned int u = 0 - 5; int i = (int)u;
Last edited by Brad0407; 03-03-2008 at 05:59 PM. Reason: code fix
Don't quote me on that... ...seriously
okay just one more issue.. you know that a mantissa is always precedeed with the 1. 0100101001 and I can't anyway extract the 1 in front of the fraction because it's actually not inside the 1, how do I append the extra one to the 0100101001?? do I shift it right first and then do an or with 1000000000 (masking technique) ?? the problem is if I do this I would lose bit 0 from the mantissa (the right most bit in the mantissa). I need to append the string so that it becomes 24 bit..
well no, after I got the one I would like to convert all that together into a hex
A possible solution....
Code:#include <stdio.h> void PrintTheBits(long lInput, int iStart, int iEnd) { unsigned int uiMask = 1 << iStart; unsigned int uiEndMask = 1 << iEnd; while (uiMask >= uiEndMask) { putchar ((uiMask & lInput) ? '1' : '0'); uiMask >>= 1; } } int main(void) { float fInput = 56.43f; long lLongValue; lLongValue = *((long *)(&fInput)); PrintTheBits (lLongValue, 31, 31); putchar ('.'); PrintTheBits (lLongValue, 30, 23); putchar ('.'); PrintTheBits (lLongValue, 22, 0); putchar ('\n'); return 0; }
If you want to print the bits that are there then print the bits that are there as a hex integer using %x, which will print as hex.
If you want to print "what it means", then remember: the bits that you have come after the binary point, and the implicit one comes before, so it's really "1."001010011100 or whatever. (You can also print that in hex as 0x1.29c or so.)
There is no context in which you would want to add the implicit one as a bit in front and then print the result as a hex integer.