Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mr Cucumber

Bitmasking with floats and doubles

This topic is 5564 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What I want to do is to take a float and bitmask out each of the for bytes in the float and save each byte in a char. This is how I tried to do it:
float bytes = whatever;
      //Filter out the bytes with a binary mask

	char byteLowest = static_cast<char> (bytes & 255);
	char byteSecondLowest = static_cast<char> ((bytes & 65280) >> 8);
	char byteSecondHighest = static_cast<char> ((bytes & 16711680) >> 16);
	char byteHighest = static_cast<char> ((bytes & 4278190080) >> 24);
Apparently the compiler dont like binary operations on floats. What can I do to perform what I want?

Share this post


Link to post
Share on other sites
Advertisement

float myfloat = whatever;
unsigned int myuint = reinterpret_cast<unsigned int&>(myfloat);
...then use bit operations to get the chars from the unsigned int.

Share this post


Link to post
Share on other sites
quote:
Original post by Mr Cucumber
Is the point of reinterpret_cast to maintain the bits in the float the exact same way in the int?
Yeah; it''s one way to reinterpret the underlying bits. Such reinterpretation can also be done with a union, or with a C-style cast, as billybob pointed out.

Share this post


Link to post
Share on other sites
Im trying to step away from the C ways of doing things. I realised recently that I do a lot of things in deprecated ways. Thanks all anyway.

Share this post


Link to post
Share on other sites
if & isn''t between a type and a name, it is used to get the adress of the variable right to it...

Share this post


Link to post
Share on other sites
to take the bits out of a float/double , you cant cast it to something else , because cast tries to change the varible to the new type , like if you cast a float like 12.42 to an int , you will get 12 !.

they showed you how to do it and i will explain :
take the float "f" , now the its address "&f" , now you have pointer to the place where the float is in memory , now you tell the compiler that the pointer is not pointing to a float but to and int (the same number of bits) "(*int)&if" .
ok now we want the actuall bits and not a pointer so we tell the compiler we want whats in that address

"int i=*((int*)&f)" , this is little ugly so you can do this in nicer way like this

union
{
float f;
char c[4];
} floatchar;

floatchar.f=1.3242;

printf(floatchar.c[0]);
printf(floatchar.c[1]);
...

you get the idea , enjoy

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!