• C

Changing sign bit of float number

I have the following code in Win32 application:

float* p = pReferenceImage;

for ( int j = 0; j < nImageSize; j++ )
{
    if ( *p < 0 )
      *p = -*p;
    p++;
}

It sets absolute value for each member of float array. The following code makes the same and seems to give right results:

float* p = pReferenceImage;
DWORD* pDW = (DWORD*) p;

for ( int j = 0; j < nImageSize; j++ )
{
  *pDW++ &= 0x7FFFFFFF;
}

Is this 100% right - setting 0 to bit 31 of float makes this number positive? I didn't understand this from float format specification.
LVL 48
AlexFMAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddunleaCommented:
Hi AlexFM,

Quoting this page [http://docs.sun.com/source/806-3567/data.represent.html]

      32 bits (four bytes or one word), aligned on a four-byte boundary. A float has a sign bit, 8-bit exponent, and 23-bit fraction.

So yes, it would appear that bit 31 is the sign bit and that clearing it indicates a positive value.

The only possible exception I can see is 0xFFBFFFFF, seeing as 0x7FBFFFFF is defined as NAN.

0
PaulCaswellCommented:
Even if it is 100% correct for that particular implementation you are making yourself implementation specific, compiler specific and all sorts of other stuff that you probably dont want.

Have you tried using fabs?

Paul
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexFMAuthor Commented:
Using fabs is a good idea, it performs like second version (with DWORD), and better than first version. However, I can get better performance using 0x7FFFFFFF way with MMX, this is a reason I ask whether this is correct. Portability, compiler and platform independency are not important for me in this case.
I know that changing of sign bit in integer is wrong, and I am not sure about float. My tests show that this formula gives right results, but I cannot test all possible float values :-)
0
AlexFMAuthor Commented:
Well, I tested DWORD + MMX version, and it gives only small performance boost relatively to fabs. So, using fabs is optimal.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.