• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java and subtraction

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does Java handle subtraction? Does it just apply the '-' unary operator and add the two operands?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
How does Java handle subtraction? Does it just apply the '-' unary operator and add the two operands?



in java i think like other languages substraction is done by 2's complement technique.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe this is right, it really does addition by flipping the bits, or whatever it's doing exactly, never quite understood the whole thing. Doesn't it flip the bits and add one or something like that?
Jason
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get the negative of a number by reversing all the bits and adding one, that's true. But that's not subtraction.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps the bytecode will give you a better insight. Which brings me to another question: Peter Haggar shows some bytecode listings in his book "Practical Java". Is there a program we can use to display similar listings? I know about hex editors and these are not the programs I had in mind since Peter's listings were nicely formatted and broken down in logical steps.


------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java's grammar, like the other languages I've seen the grammars of, distinguishes between the unary minus operator (negation) and the binary minus operator (subtraction). Although they are represented by the same token ('-'), they are two different operators. I have not examined the bytecode but would not expect the binary operator to use negation and addition.
Junilu, the SDK ships with the "javap" tool which disassembles to bytecode if you use the -c flag. If you want something faster, try dis. In addition, most Java decompilers can disassemble as well. Many people use JAD; I'm quite fond of the DJ Java Decompiler which is a (Windows) graphical shell around JAD.
- Peter

[This message has been edited by Peter den Haan (edited October 31, 2001).]
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Peter. I've seen JAD and DJ but I haven't really used them.
javp disassembly actually shows two different ops: isub and iadd. So, it seems that the answer is that subtraction and addition are treated differently.
Junilu
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a exam prep book I am reading:

The subtraction operation is equivalent to adding a binary number and the 2's complement of the other number.


The problem with this statement is are they saying that yes, this is how subtraction is being done, or this is how you can think of subtraction being done?
Jason
 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds to be like they are multiplying it by a (-1) and then adding it. 2's compliment is multiplying it by -1.
2's compliment is basically reversing the bits and adding 1. You need to do that in 2's compliment to account for the zero. The zero takes up 1 position. Otherwise flipping the bits will be off by 1 number.
-Dale
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
[This message has been edited by Dale DeMott (edited November 01, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaah, I understood the process that was behind it, but not the reasoning. Imagine that.
Still, is that how Java is doing it, or what he is comparing it to? If that is how, then Java handles subtraction by really doing addition... or have I lost what we were discussing
Jason
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given Junilu's findings that's not how Java is doing it at the bytecode level - note the book's use of the words "equivalent to" rather than "implemented by".
The JVM itself may still implement the isub operator using negation and addition. It's not likely though. Every CPU I know has a native subtraction operation. The Intel x86 compatible ones certainly do.
- Peter
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, inside the processor, there's an excellent chance that the way the subtraction operation is implemented is by converting to twos complement and performing an addition. It's just more efficient to have a single instruction to do this, since each instruction takes nonzero time to load and interpret. Or perhaps there is a fundamentally different way to implement it internally which I don't know - my intro to course in digital logic probably didn't cover all the tricks that are used in modern processors, after all.
OK, I admit that Marilyn's question was how Java implements subtraction, not how the processor does it. Still, at some level it probably still boils down to adding a negative.
[This message has been edited by Jim Yingst (edited November 02, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic