6 ms·
Loopy C Puzzle (2011)
- smlckz 6y agoI'd suggest that instead of all of those ''\( \oplus \) denote bitwise XOR operation on two nonnegative integers'', put that before all your maths into a ''Notation: '' line so that you do not have to repeat yourself.
- jonsen 6y agoand I’d suggest delete “on two non-negative integers” which is irrelevant for bitwise operations.
- lvoudour 6y agoAn obvious solution is to change i-- to i++. Don't know if OP is the author, but the code under the statement is not the correct one
- susam 6y agoThanks! That was a copy-paste error. Fixed it now.
- Thorrez 6y ago>The value in variable i decrements to INT_MIN after |INT_MIN| + 1 iterations. Wouldn't it be just after |INT_MIN| iterations?
- tomsmeding 6y agoRunning from 0 to -6 is 7 iterations. Running from 0 to INT_MIN is |INT_MIN| + 1 iterations.
- Thorrez 6y ago>Running from 0 to -6 is 7 iterations. I guess it depends on your definition of an iteration. I view an iteration as including the iteration expression (i-- in this case) at the end. So during 7 iterations, i-- has executed 7 times, so the value of i is -7. You seem to be saying the iteration expression belongs to the next iteration as opposed to belonging to the currently ending iteration. I think my view is the widely accepted view. Check out how cppreference rewrites a for loop: { init_statement while ( condition ) { statement iteration_expression ; } } In our specific case: { i = 0; while (i < 6) { printf("."); i--; } } https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/for Also the article doesn't say "running |INT_MIN| + 1 iterations" it says "after |INT_MIN| + 1 iterations". I think in that case, even if you don't consider the final i-- to be part of the "n + 1 iterations", you would still consider the final i-- to be done "after |INT_MIN| + 1 iterations".
- susam 6y ago>> Running from 0 to -6 is 7 iterations. > I view an iteration as including the iteration expression (i-- in this case) at the end. Then you should agree that running from 0 to -6 (inclusive) is 7 iterations. Example: #include <stdio.h> int main() { int i; for (i = 0; i >= -6; i--, printf("iterated\n")) { printf("i = %d\n", i); } return 0; } Output: i = 0 iterated i = -1 iterated i = -2 iterated i = -3 iterated i = -4 iterated i = -5 iterated i = -6 iterated
- deleted 6y ago[deleted]
- MaxBarraclough 6y agoCustomary links to the 'required reading' blog posts on undefined behaviour in C: * https://blog.llvm.org/posts/2011-05-13-what-every-c-programmer-should-know/ https://blog.llvm.org/posts/2011-05-13-what-every-c-programm... * https://blog.regehr.org/archives/213 https://blog.regehr.org/archives/213 * https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=633 https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63...
- noncoml 6y agoDave, if I see this in one of your PRs, I promise I will lose it man and we will end up in HR
- keyle 6y agoI don't know who Dave is but I like him already.
- deleted 6y ago[deleted]