I don't understand exactly what you're trying to do, but if you need to do different things depending on the sign of a variable, you can just check if the variable is less than zero or not with an if-statement.
And yes the program is doing what is supposed to do! !!
Joined: Mar 17, 2005
Jesper de Jong wrote:I don't understand exactly what you're trying to do, but if you need to do different things depending on the sign of a variable, you can just check if the variable is less than zero or not with an if-statement.
Hi Jesper & Trivedi,
Thanks for the reply.
My requirement is:
At any point of time, the mathematical operation should be subtraction and if and only if num1 is -ve, then, sign of the total should be -ve.
Is there any better way to do this ? any suggession for performance turing?
Anu satya wrote:
I have given the code now. Please check my previous post.
Having read this thread, and looked at your code, I agree with everyone -- I don't know why you are looking for the sign bit, when a simple comparison with zero will do.
But to answer your question.... with the IEEE 754 double precision format, the sign bit is the left most one. Just use the Double.doubleToRawLongBits() method to get the bits, and check the left one. This does seem much harder than just checking for less than zero, don't you think?
I presume you would use the & operator with 0x8000000000000000L and check whether the result is or is not 0, Henry, though I agree < 0 seems much more sensible. There are also methods called signum which you should seek in the API documentation.