Tespa42
Tespa42

Reputation: 577

How does one compute the equality of two integers on the JVM without branching?

On the JVM, is there a way to compute whether two integers are the same without using a branching instruction? Is there a way to cleverly use the ixor instruction to do so? If there is a way, is it even worth using instead of the branching version?

Upvotes: 0

Views: 74

Answers (1)

Michael Kay
Michael Kay

Reputation: 163332

What's wrong with doing a subtraction?

I guess it depends what outcome you want. If getting 0 on the stack to mean equal and non-zero to mean not equal isn't the outcome you are after, then you need to explain your requirements better. Clearly if you want to execute different code in the two cases, then you will need to branch.

Upvotes: 2

Related Questions