Still not sure what you're on about, but it's just bits. You can set any of them. Or you zero them out, both as you like. You can hide some bits in there if you want, of course, but they'll also be part of the number.
Do you mean the sign bit? If you've already extracted it, then you know whether the number is supposed to be positive or negative. You can then multiply appropriately.
Edit: or do you mean the implicit 1 in front of the mantissa? It's not stored with the number (hence implicit), so you can't extract it. The way to check if it's supposed to be there is to extract the exponent: if the exponent is 0, the number is denormalized so there's no implicit 1.xxx, otherwise there is.
Last edited by tabstop; 03-03-2008 at 11:08 AM.
is there anyway to append bits in C?? because the exponent is located between bits 23-30 and I need to get that bits and transformed it into hexadecimal.. how would I do this?
and how would I move one bit to the other?? if I add one it will add an integer 1 not a bit
Last edited by -EquinoX-; 03-03-2008 at 11:12 AM.
You can convert it or print it as hex.
printf("%x", mantissa)
For example.
You move bits using shift >> and <<. As in my code I posted.
You can set bits using binary OR.
You can zero bits using a combination of binary AND and XOR:
Code:char c = 0xFF; c & ~(0x8); // Zero fifth bit
here's the code to find the exponent, is this right??
Code:#include <stdio.h> #define SIGN_BIT (1U << 31) #define EXPONENT (11111111U << 31) int main() { float first = 56.43; int *second; unsigned int sign; unsigned int mantissa; second = (int *) &first; sign = (*second) & SIGN_BIT; /* note one ampersand for bitwise-and */ sign = sign >> 31; mantissa = ((*second) << 1) & EXPONENT; mantissa = mantissa >> 23; printf("%u\n", sign); printf("%u\n", mantissa); return 0; }
Exponent is correct. Mantissa is way off.
I'll help you, though. The easiest way to construct a proper mask to use is first to write down, in binary, the entire mask. I'll take the sign bit as example:
10000000000000000000000000000000 (31th bit signed, rest 0).
Use Windows Calculator (you are using Windows, I hope?), select scientific mode, select binary, past your mask there.
Select hex and copy your new mask.
Doing that gives me 80000000.
So I do nTemp & 0x80000000 and I get the sign bit. The same hold true for the other parts as well.
Then shift into position.
oh I am sorry, I meant exponent here
11111111U is not eight consecutive bits. If you want eight consecutive bits, the easiest way (I think) is to use hex notation: 0xff. Also note that you don't want to shift 31 places, because the exponent is not 31 places away from where you start.
so is this right now:
Code:#include <stdio.h> int main() { float first = -10.000000; int *second; unsigned int sign; unsigned int exponent; unsigned int mantissa; second = (int *) &first; sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */ sign = sign >> 31; exponent = ((*second) << 1) & 0xff000000; exponent = exponent >> 23; mantissa = ((*second) << 9) & 0xfffffe00; mantissa = mantissa >> 9; printf("%u\n", sign); printf("%u\n", exponent); printf("%x\n", mantissa); return 0; }
Last edited by -EquinoX-; 03-03-2008 at 11:55 AM.
You don't ever ever ever ever ever ever ever ever ever ever want to shift *second.
As to your bit masks, do you know how to convert binary to hex? Let me take a completely arbitrary example: let's suppose I want to use the bitmask 11000110001100011000110001100011. I would break it up into groups of four:
1100 0110 0011 0001 1000 1100 0110 0011. I can then turn each group of four into a single hex digit: 12 6 3 1 8 12 6 3 -> 0xc6318c63. So write down your bitmask for exponent and mantissa and convert it to hex.
oh so we don't want to shift second?
Exponent is right. You did that excellent.
Mantissa is correct, by chance I believe.
Exponent is not correct.
You don't need to shift the actual number. Shift the mask or construct the correct mask in the first place.
It's far easier to just use a calculator to do the job. And there's nothing wrong with it, either.
Windows provides an excellent tool for this. I'm sure other OSes do too.
I'd avoid it, if I were you. You're essentially destroying original data by doing that.
In this case, you can get away with it, but next time you might not be so lucky.
I've revised the code so that I won't have to change second, let me know if I messed up.
and how do I directly eliminate the excess -127 in the exponent??Code:#include <stdio.h> int main() { float first = -10; int *second; unsigned int sign; unsigned int exponent; unsigned int mantissa; second = (int *) &first; sign = (*second) & 0x80000000; /* note one ampersand for bitwise-and */ sign = sign >> 31; exponent = (*second) & 0x7F800000; exponent = exponent << 1 exponent = exponent >> 23; mantissa = (*second) & 0x7FFFFF; mantissa = mantissa << 9; mantissa = mantissa >> 9; printf("%u\n", sign); printf("%u\n", exponent); printf("%x\n", mantissa); return 0; }
Well done. You have the right mask, but you need to stop needlessly shifting around the numbers.
Shift them right so that the first bit in the number becomes the 0th bit in the whole number.
Shift it into the right place and you don't have to worry about "excess" data. It will be as it should be.