Order of evaluation of java expression -
so have arraylists represent integers example num1 = <-1,4,5>
represent -145 , num2= <2,3,6>
represent 236.
arraylist<integer> num1 = new arraylist<>(arrays.aslist(1,2,9)); arraylist<integer> num2 = new arraylist<>(arrays.aslist(-5,5)); final int sign = num1.get(0) < 0 ^ num2.get(0) < 0 ? -1 : 1;
so sign supposed determine sign occur multiplying num1 num2 (-145*236
negative number). comparison operators non associative should equivalent expression:
sign = (num1.get(0) < 0 ^ num2.get(0)) && (0 ^ num2.get(0) < 0) ? -1:1;
whats confusing me why num2.get(0)
being xor'ed 0 because not alter num2.get(0)
@ all.
also lets num2.get(0) = -2
, num1.get(0) = -3
if evaluated sign we'd sign=-1. because (-3< 0 ^ -2) , (0 ^ -2 < 0)? -1:1;
evaluates -1.
but wrong -3*-2 should positive number. missing because know code correct (it's textbook).
num1.get(0) < 0 ^ num2.get(0) < 0 ? -1 : 1
is evaluated as
((num1.get(0) < 0) ^ (num2.get(0) < 0)) ? -1 : 1
this determined operator precedence. comparison operators being non-associative has nothing whatsoever.
whats confusing me why num2.get(0) being xor'ed 0
it isn't.
Comments
Post a Comment