Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementor logic

Tags:

I'm trying to get deeper with post and pre incrementors but am a bit stuck with the following expression :

public static void main(String[] args) {
    int i = 0;
    i = i+=(++i + (i+=2 + --i) - ++i);
    // i = 0 + (++i + (i+=2 + --i) - ++i);
    // i = 0 + (1   + (3    +   2) -  1 );
    // i = 0 + (6                  -  1 );
    System.out.println(i); // Prints 0 instead of 5
}

I know I'm missing the logic somewhere but where?

What I've tried :

  • Going from left to right (though I know it is not recommended)
  • Going from the insidest bracket and starting from there.

Thanks for the help

PS : The comments are the details of my calculus

EDIT 1

I tried to change de hard coded value from the expression from 2 to something else and the result always gives 0

Look at this example :

    int i = 0;
    i = i+=(++i + (i+=32500 + --i) - ++i);
    System.out.println(i); // Prints 0

This expression should logically be nowhere near 0 but somehow it does print it.

The same happens when I use a negative :

    int i = 0;
    i = i+=(++i + (i+=(-32650) + --i) - ++i);
    System.out.println(i); // Prints 0

EDIT 2

Now, I changed the value of i to begin with :

    int i = 1;
    i = i+=(++i + (i+=2 + --i) - ++i);
    System.out.println(i); // Prints 2
    
    i = 2;
    i = i+=(++i + (i+=10000 + --i) - ++i);
    System.out.println(i); // Prints 4
    
    i = 3;
    i = i+=(++i + (i+=(-32650) + --i) - ++i);
    System.out.println(i); // Prints 6

It gives the double of i each time, whatever the hard coded value is.

like image 336
Yassin Hajaj Avatar asked Oct 14 '15 08:10

Yassin Hajaj


People also ask

What is Incrementer and Decrementer?

Incrementer and decrementer circuits are used to increment or decrement a binary data by unity. These are based on combinational logic blocks. In this section, the introductory concepts of 3-bit incrementer and decrementer are deliberated. A 3-bit binary can be easily incremented by adding unity.

What is binary incrementer unit?

The increment micro-operation adds one binary value to the value of binary variables stored in a register. For instance, a 4-bit register has a binary value 0110, when incremented by one the value becomes 0111.


2 Answers

Quoting Java Language Specification, 15.7 Evaluation Order:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.

So, essentially, i += ++i will remember the old value of i on the left side, before evaluating the right side.

Remember, evaluation order of operands and precedence of operators are two different things.

Showing evaluation order, step by step, with saved value in {braces}:

int i = 0;
i    = i    += (++i + (i    += 2 + --i) - ++i); // i = 0
i{0} = i    += (++i + (i    += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (++i + (i    += 2 + --i) - ++i); // i = 0
i{0} = i{0} += (1   + (i    += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1   + (i{1} += 2 + --i) - ++i); // i = 1
i{0} = i{0} += (1   + (i{1} += 2 + 0  ) - ++i); // i = 0
i{0} = i{0} += (1   + (i{1} += 2      ) - ++i); // i = 0
i{0} = i{0} += (1   + 3                 - ++i); // i = 3
i{0} = i{0} += (4                       - ++i); // i = 3
i{0} = i{0} += (4                       - 4  ); // i = 4
i{0} = i{0} += 0                              ; // i = 4
i{0} = 0                                      ; // i = 0
0                                             ; // i = 0

Followup to edits to question

If we name the initial value I and the constant N:

int i = I;
i = i += (++i + (i += N + --i) - ++i);

Then we can see that the values are:

i{I} = i{I} += ((I+1) + (i{I+1} += N + I) - ((I+1+N+I)+1));
i{I} = i{I} += (I + 1 + (I + 1 + N + I) - (I + 1 + N + I + 1));
i{I} = i{I} += (I + 1 + I + 1 + N + I - I - 1 - N - I - 1);
i{I} = i{I} += I;
i{I} = I + I;
i = 2 * I;
like image 185
Andreas Avatar answered Sep 22 '22 14:09

Andreas


This is the logic taking into account your first edit (with an unknown X):

public static void main(String[] args) {
    int i = 0;
    i = i+=(++i + (i+=X + --i) - ++i);
    // i = 0 += (++i + ((i += (X + --i)) - ++i));
    // i = 0 += (1 + ((i += (X + --i)) - ++i)); // i = 1
    // i = 0 += (1 + ((1 += (X + --i)) - ++i)); // i = 1 and i will then take the result of 1 += (X + --i)
    // i = 0 += (1 + ((1 += (X + 0)) - ++i)); // i = 0 and i will then take the result of 1 += (X + 0)
    // i = 0 += (1 + (X + 1 - ++i)); // i = X + 1
    // i = 0 += (1 + (X + 1 - X - 2)); // i = X + 2
    // i = 0 += (0); // i = X + 2
    // i = 0;
    System.out.println(i); // Prints 0
}

Tricks here:

  • += is an assignement operator so it is right-associative: in the snippets, I added parenthesis to express this more clearly
  • The result of the assignment expression is the value of the variable after the assignment has occurred
  • The postfix increment operator ++ and postfix decrement operator -- add or subtract 1 from the value and the result is stored back into the variable.
  • The + additive operator first computes the left-hand operand then the right-hand operand.

For your second edit (with an unknown I added):

public static void main(String[] args) {
    int i = I;
    i = i+=(++i + (i+=X + --i) - ++i);
    // i = I += (++i + ((i += (X + --i)) - ++i));
    // i = I += (I+1 + ((i += (X + --i)) - ++i)); // i = I+1
    // i = I += (I+1 + ((I+1 += (X + --i)) - ++i)); // i = I+1 and i will then take the result of I+1 += (X + --i)
    // i = I += (I+1 + ((I+1 += (X + I)) - ++i)); // i = I and i will then take the result of I+1 += (X + I)
    // i = I += (I+1 + (X+2*I+1 - ++i)); // i = X + 2*I + 1
    // i = I += (I+1 + (X+2*I+1 - X-2*I-2)); // i = X + 2*I + 2
    // i = I += (I); // i = X + 2*I + 2
    // i = 2 * I;
    System.out.println(i); // Prints 2 * I
}
like image 42
Tunaki Avatar answered Sep 22 '22 14:09

Tunaki