So what have you got so far as to your code?
--
Mats
So what have you got so far as to your code?
--
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.
The bits are already stored in the number. Otherwise machines would not be able to understand it. To extract the specific values you would use both bitwise & (AND) and binary shifts (<<, >>).
You would have to construct a mask to isolate the parts of the number that form the mantissa and the exponent.
Wrong here, the 32nd bit is the exponent but bits are indexed 0..31 in a 32 bit number.yes I know how floating points are layed out in binary the 31st bit is the sign bit and 23-31 is the mantissa and 0-23 is the exponent.
[31 SIGN][30-23 EXPONENT][22-0 MANTISSA]
Now for the masking part. A mask is a series of 1111 in a machine word usually positioned at the right place so that when AND'ed with a number they zero out all the bits we don't care about.
If you want the sign bit, where should the 1's be in a 32 bit unsigned integer mask and how many of them also?
If you can answer that question for the EXPONENT and the MANTISSA i might guide you to code it in C.
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);
okay let me get this clear first, say I have a float in C which is 56.43 the 56.43 are already stored in the computer and all I need to do is to extract them by the masking techniques right??
so for a 32 bit if I want the sign bit it should be on the 31 bit?
okay I get it, so therefore I don't have to convert the floating point to a binary first and then find the mantissa and so on. I just have to extract the right one because it's already layed out that way in the memory?
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.
okay so I got the idea now, how do I do the AND sign at?? do I have to find the location of the memory where 56.43 is stored?? using pointers?? 56.43 is a stored in memory as a 32 bits right??
If it's a float, yes. And one way to get to the float is by pointer, another solution is to overlay it with a union of integer and float.
Also, you can avoid the AND-business altogether and use a bitfield struct - although that will make it more compiler dependant.
--
Mats
Last edited by matsp; 03-03-2008 at 10:53 AM.
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.
pointer seems to be a great idea say that I do this in C:
float number = 56.87
int* point = &number
then point will point to the address of the sign bit of the float?
You probably need a cast to make the compiler happy, but yes, that's the idea.
--
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.
I just wrote this simple code:
I am trying to get the sign bit here, but somehow it gets a very large numberCode:#include <stdio.h> int main() { float number = 56.73; int* sign_pointer = (int*) &number; int temp = *sign_pointer; printf("%p\n", sign_pointer); printf("%d\n", temp); return 0; }
Of course, you're just putting the whole data into temp.
This is what -56.43 is in binary (sign, exponent, mantissa):
1 10000100 11000011011100001010010
So number containers
1 10000100 11000011011100001010010
Then the value of number is assigned to temp, so temp becomes
1 10000100 11000011011100001010010
You need to use binary and to extract the bits you want. Then you need to shift the bits into the correct position to get a valid result.
Btw, consider using a negative number. So when you get 1, you know that you got the right bit, since the sign bit is set when the number is negative.
could you give me an example of using this and binary to extract the bits in C code, because I don't quite get it. an example might really help.. thanks
Binary AND works with a mask.
It's simple, really...
Quote from MSDN.The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
So say you have this
This sample will extract the 5th bit. But if you want to use it, you want it in the correct position, because the number will be 0x8, not 0 or 1.Code:char c = 0xFF; char mask = 0x8; char c2 = c & mask;
This code shifts the number right 4 times, back into its rightful position as bit 0. Thus it will be 0 or 1.Code:c2 >>= 4;
Why use a pointer? The general rule for working with bits is that unsigned types are the best to use even though it really doesn't matter, it is much better when viewed hexadecimally.
Try this see what it outputs. Binary and hexademical arithmetic are not taught in school usually but they are not that tough to understand... Numbers are absolute quantities that can be represented in various ways.Code:float number = 56.73f; unsigned int sign_mask = 0x80000000; /* Bit 31 */ /* Alternative option */ unsigned int sign_mask = (1U << 31); /* The same as above */ unsigned int sign_bit; sign_bit = number & sign_mask; /* sign_mask is : 1000 0000 0000 0000 0000 0000 0000 0000 in binary therefore, sign_bit is either all zeros or like sign mask in binary, which is a large number of course. */ printf("%#08x\n", sign_mask); printf("%#08x\n", sign_bit);
A typical example of ...cheap programming practices.Code:... goto johny_walker_red_label; johny_walker_blue_label: exit(-149$); johny_walker_red_label : exit( -22$);