Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# How to get the sign bit?

Anu satya
Ranch Hand

Joined: Mar 17, 2005
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

With Regards,
Anu
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1499
why are you using abs()?

And I dont get why

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

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Anu satya
Ranch Hand

Joined: Mar 17, 2005
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
Bartender

Joined: Aug 16, 2005
Posts: 13554

6

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1499
And yes the program is doing what is supposed to do! !!
Anu satya
Ranch Hand

Joined: Mar 17, 2005
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
Bartender

Joined: Aug 16, 2005
Posts: 13554

6

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

Joined: Mar 17, 2005
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

Joined: Sep 28, 2004
Posts: 17629

33

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
Sheriff

Joined: Oct 13, 2005
Posts: 34675

14
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.

I agree. Here's the link: http://aspose.com/file-tools

subject: How to get the sign bit?