Changing sign bit of float number

Posted on 2004-11-18
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;

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.
Question by:AlexFM
    LVL 5

    Expert Comment

    Hi AlexFM,

    Quoting this page []

          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.

    LVL 16

    Accepted Solution

    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?

    LVL 48

    Author Comment

    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 :-)
    LVL 48

    Author Comment

    Well, I tested DWORD + MMX version, and it gives only small performance boost relatively to fabs. So, using fabs is optimal.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now