Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the sign bit?  RSS feed

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

can any one please help?

Thanks

 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()


Please help
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And yes the program is doing what is supposed to do! !!
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Please help
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!