Explanation for `check /= 2;`

java

Solution

This is the way to `go through all digits`. The most right digit is retrieved by `check % 2`, and after checking it, you shift `check` one digit (bit) to the right by `check /= 2` (equals to `check = check / 2;`)

Problem

I need some explanation for this code. This is the example code given by others. ``` for ( int i = 1; i <= 8; i++ ) { if(check % 2 == 0) sum += i; else sum -= i; check /= 2; <--- Need explanation for this line. } ``` But in the `Pseudo code`, there is no `check /= 2;` procedure. Here is the full `Pseudo code`. ``` int binary = 0; int sum; while(binary<256) sum = 0; for(go through all 8 digits) if the i-th digit is 0 sum += i if the i-th digit is 1 sum -= i end for if sum == 0 output binary++ end while ``` So, what is the purpose for that line of code? Since `sum`, `binary`, and `check` is initialize as `0`. I have written this code using the Pseudocode given above. But seems like my code will duplicate the output and one more problem, the format. I want the output be like this format: ``` Enter a number : 3 -1 -2 +3 = 0 1 +2 -3 = 0 ``` But my currently output is: ``` Enter a number : 3 -1 -2 3 = 0 1 2 -3 = 0 ``` Here is my code: ``` CODE IS REMOVED! ``` Solved! I'm too focus on the `for`-loop for the output part, hence miss the `while`-loop for the binary, because the pseudocode is for 256 possible solutions, hence, there will be same output for the front part, example: ``` 1 - 2 - 3 + 4 = 0 1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 = 0 ``` Hence, the pseudocode may give an same output. So, since the solution is in `2 ^ n` where `n = 1, 2, 3, ...` form, so change the ``` while( binary < 256 ) ---> while ( binary < Math.pow(2, input)) ``` should solve it. The format and the duplicate of the answer are solved.

Original source