I was given homework to write a program that finds if a double (64 bit) is negative. The double uses twos complement, so the first bit will tell if it is positive (0) or negative (1), but you all probably know that.

The problem I am having is that there can be no floating point operations, just bit masking and shifting. The function can be written with no if statements. It should return 1 if it is negative and 0 if it is not. I thought about masking just the sign bit. I also thought about shifting the sign bit all the way through. Is there any way to just access the first bit and use that in the return statement.

Thanks for any help!

0x8000000000000000 is the 'sign bit' of a 64 bit value. AND it with the value you want to check. If TRUE, the sign bit is set.

decalre a union with one double value and char array of 8 bytes.
in Little Endian format , always the sign will be MSB.
so take 7th array, and perform AND operation with 1(char) left shifted 7 times.
and return the resultant value.

NOTE: the returned value is not 1 here but is 128 in case of -Ve.
not sure its efficient.

AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.

Please correct me if i am wrong.
i'm too eager to know, other efficient methods.

below code is just an example .

int getsign( );
union sign {
        double d1;
        char d[8];
  }u;

int main()
{
 u.d1 = -16.32;

 if(getsign())
        printf("\n %lf is -Ve\n",u.d1);
 else
        printf("\n %lf is +Ve\n",u.d1);

 u.d1 = 16.32;

 if(getsign())
        printf("\n %lf is -Ve\n",u.d1);
 else
        printf("\n %lf is +Ve\n",u.d1);

 return 0;
}

int getsign(double d1)
{
  char m=1;
  return m<<7 & u.d[7];
}

As requested, Gaiety:

decalre a union with one double value and char array of 8 bytes.
in Little Endian format , always the sign will be MSB.
so take 7th array, and perform AND operation with 1(char) left shifted 7 times.
and return the resultant value.

NOTE: the returned value is not 1 here but is 128 in case of -Ve.
not sure its efficient.

Way too complex for a new programmer...

AND ing with 0X8000000000000000 is good idea but bit wise operations on float value is not valid.

Not true. What is true is ANDing with a float's maintissa or exponent and getting a useful result is difficult to impossible, but the sign bit is a defined bit in a defined location so it's perfectly valid. If I'm wrong, please point (link) me to the rules & explanation.

Please correct me if i am wrong.
i'm too eager to know, other efficient methods.

Done... :icon_wink:

As requested, Gaiety:

Way too complex for a new programmer...


Not true. What is true is ANDing with a float's maintissa or exponent and getting a useful result is difficult to impossible, but the sign bit is a defined bit in a defined location so it's perfectly valid. If I'm wrong, please point (link) me to the rules & explanation.


Done... :icon_wink:

could you please provide me a small code snippet on how that can be done?
do we need to use pointer stuff..!

int main()
{
        double nums[] ={16.32,-16.32};
        int i=0;
        while(i<2)
        {
        if(getsign(&nums[i]))
                printf("%lf is -ve\n",nums[i]);
        else
                printf("%lf is +ve\n",nums[i]);
        i++;
        }

return 0;
}

int getsign(double *d1)
{
        char *p;
        p=(char *)d1;
        p=p+7;
        return *p & 1<<7;
}

Mmm ... interesting. I always thought that bitwise operators were only valid with integral operands. The only way I can think of using bitwise operators with floats/doubles is to use casting pointers. Perhaps something like:

int isNegative(float f) {
    int *ip = (int*)(&f);
    return (*ip & 0x80000000 ? 1 : 0);
}

But then, this may also be too complex for a new programmer.

Can you explain this last line. I understand the bit wise 'and' to isolate the bit, but I am lost on the last line. (return statement)

return (*ip & 0x80000000 ? 1 : 0);

Thanks for your help

Can you explain this last line. I understand the bit wise 'and' to isolate the bit, but I am lost on the last line. (return statement)

return (*ip & 0x80000000 ? 1 : 0);

Thanks for your help

The example I posted demonstrates the use of a ternary operator. Here's a brief summary on how the ternary operator works:

http://cprogramminglanguage.net/c-ternary-operator.aspx

Essentially it is a short form equivalent of the if-else construct. I posted it this way because your original post stated that you weren't permitted to use any 'if' type statements.

Have a read and post back if you still have questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.