?
Solved

Changing sign bit of float number

Posted on 2004-11-18
4
Medium Priority
?
389 Views
Last Modified: 2012-06-22
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.
0
Comment
Question by:AlexFM
  • 2
4 Comments
 
LVL 5

Expert Comment

by:ddunlea
ID: 12613082
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
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 2000 total points
ID: 12613218
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
 
LVL 48

Author Comment

by:AlexFM
ID: 12613560
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
 
LVL 48

Author Comment

by:AlexFM
ID: 12613887
Well, I tested DWORD + MMX version, and it gives only small performance boost relatively to fabs. So, using fabs is optimal.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question