Compare the sign of two two-local

What is the fastest way to compare a mark on double?

I know that it doublehas a โ€œsign bitโ€, but I'm not sure if the way to โ€œlook for itโ€ in its binary representation is a good idea or not.

Troubleshooting portability issues, can someone tell me what is happening with this code in MSVC ++?

#include <stdio.h>

int main()
{
  double z = 5.0 ;

  __int64 bitSign ;

  __int64 *ptr ;

  ptr = (__int64*)&z ;

  for( __int64 sh = 0 ; sh < 65 ; sh++ )
  {
    bitSign = 1L << sh ; // Weird.  it doesn't do 1.
    printf( "Bit# %d (%llx):  %lld\n",
      sh, bitSign, ( (*ptr) & bitSign) ) ;
  }

}

Firstly, why does it start with bit 32, although I only moved one bit?

Secondly, is it okay for me to check the 64th bit doubleto check its sign in MSVC ++?

+3
source share
4 answers

At a minimum, there are three comparisons that are: possibly

  • extract sign
  • b
  • a b

.

and, less than, , - , , // -. . . - , :

__int64 mask = 0x8000000000000000; // i think that the right amount of 0 :-)
if( (x&mask) ^ (y&mask) ) 
  puts("different");
else
  puts("same");

xor'ing . , xor 0 (false) - , xor 1 (true). , , , .

: , " ", ... , , . :

  • .
  • ( ) .

, , , , .: -)

+2
((((__int64*)(&z))*) & 0x8000000000000000) give you the sign
0

double ( ), . +0.0 -0.0? + NaN -NaN?

0

++ 11 std::signbit, true, , false .

, a b, == !=:

void classify(double _lhs, double _rhs)
{
    std::cout << _lhs << " and " << _rhs;
    if (std::signbit(_lhs) == std::signbit(_rhs))
        std::cout << " have the same sign\n";
    else
        std::cout << " have different signs\n";
}

Live Demo


std::signbitsupports double, float, long doubleand all integral types. It can even process values NaN.

0
source

Source: https://habr.com/ru/post/1744365/


All Articles