Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# How to get the sign bit?

Anu satya
Ranch Hand
Posts: 146
Hi all,
I need to maintain the sign bit of the variable of type double.
My requirement is:

double total,num1,num2;
total = abs(num1)-abs(num2);

Examples:
1)if num1= -200 & num2=2, then total should be = -200
2)if num1 = 202 & num2 = -2, then total should be = 200

I don't know how to display the sign bit.

Thanks

Akhilesh Trivedi
Ranch Hand
Posts: 1608
why are you using abs()?

And I dont get why

if num1= -200 & num2=2, then total should be = -200 ?

Anu satya
Ranch Hand
Posts: 146
Akhilesh Trivedi wrote:why are you using abs()?

And I dont get why

if num1= -200 & num2=2, then total should be = -200 ?

sorry for the mistake.
if num1= -202 and num2 = 2, then total should be -200
I need to get the absolute value. So, I am using abs()

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
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.

Akhilesh Trivedi
Ranch Hand
Posts: 1608
And yes the program is doing what is supposed to do! !!

Anu satya
Ranch Hand
Posts: 146
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,

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?

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Anu satya wrote:Irrespective of sign on num1/num2, the mathematical operation should be subtraction and add sign of biggest number to the result.

Ok, let's translate that into pseudo-code:

• We have two numbers, a and b
• We subtract them: result = a - b
• We find out which one of a and b is the largest: max = Math.max(a, b)
• We check the sign of that number, and make sure to flip the sign of result if necessary: if ((max < 0 and result > 0) or (max > 0 and result < 0)) then result = -result;

• Now you can translate this into Java code (if this is exactly what you want - I can see some points in the requirements that might be different from what you actually meant).

Anu satya
Ranch Hand
Posts: 146
Jesper de Jong wrote:
Anu satya wrote:Irrespective of sign on num1/num2, the mathematical operation should be subtraction and add sign of biggest number to the result.

Ok, let's translate that into pseudo-code:

• We have two numbers, a and b
• We subtract them: result = a - b
• We find out which one of a and b is the largest: max = Math.max(a, b)
• We check the sign of that number, and make sure to flip the sign of result if necessary: if ((max < 0 and result > 0) or (max > 0 and result < 0)) then result = -result;

• Now you can translate this into Java code (if this is exactly what you want - I can see some points in the requirements that might be different from what you actually meant).

Hi Jesper,
I have given the code now. Please check my previous post.

please give suggessions for performance tuning.

Henry Wong
author
Sheriff
Posts: 23295
125
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?

Henry

Campbell Ritchie
Marshal
Posts: 56570
172
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.