The bitwise '&' operator checks whether the bits on the lhs and rhs are both 1:
0 & 0 = 0 0 & 1 = 0 1 & 0 = 0 1 & 1= 1
Let's take an odd number 9. In binary it writes as 1001. The output when & with 1:
1001 0001 & --------- 0001 = 1
Now, let's take an even number 8. In binary: 1000. The output will be:
1000 0001 & --------- 0000 = 0
C and C++ treats zero and one as false and true.
N.B. this also works with signed integers due their implementation using two's complement. A bit (ha) more complicated so check Ben Eater on YT for more detail.
Yes, please don't use this in a production and also not to learn programming (well maybe as a bad code example). I haven't even checked the syntax, i usually only use bash in our CI/CD deploy jobs, so not very frequently.
The while loop is insane.
Appending to a file without checking anything (available? Writeable? Storage free?) Is also bad practice.
And of course the thousand " if's " are just insane.
A simple calculation would do this. Something like this (Number % 2 == 0) ? True : false;
It's a generation script to automatically create the conditions in the if statement. Use it to generate C source that then gets compiled into the program.
Most languages don’t have something that is along the lines of IsEven() or IsOdd(), so it can’t be written out so plainly.
In C#, you’d use the modulus operator (%) to get the remainder of a division. If the value can be divided by 2 with no remainder, by definition it is even, so 'value % 2 == 0' would be true on even numbers.
Unless they know of that modulus operator, which newbies most likely won’t without a google search, you get snippets like this as best attempts. Though this post sounds more like satire than a newbie attempt, still tho that’s how these things happen
Since the user is looking for even numbers, why not just create a function to divide input by 2, then check if it's a whole number? Less than 3 lines of code
The author devised an ingenious, automated "thinking like a programmer" solution: he simply wrote a script that enumerated and then spat out code of all cases for numbers up to the largest prime humans ever used.
Language is probobly c++ due to the "private" part - and it otherwise looks like C, but it could be java - not very familiar with how syntaxes in Java looks
Nope, it's not C, and definitely isn't C++, since the private keyword works a little differently there: you write private: with the colon and after that you can write as many method and property definitions and they will all be private.
C# and Java require you to repeat the private keyword like in the image.
56
u/mohragk Aug 10 '21
Don't even need modulo:
19
u/notAnotherJSDev Aug 10 '21
Imagine thinking this was anything other than satire.
8
u/Nassiel Aug 10 '21
Man, this is kat maddox Tweet, and she wrote it ironically like all her account about programming... https://twitter.com/ctrlshifti/status/1288745146759000064?s=19
3
u/Ok-Cow5671 Aug 14 '21
Legends says that is still writhing
81
u/Akantor47 Aug 10 '21
Wtf? Just use a bash script?
counter=0
while(true){
echo "if (number == $counter) return true;" >> Programm.txt
counter +=1
echo "if (number == $counter) return false;" >> Programm.txt
counter += 1
}
Just don't forget to stop it after a few hours.
Sorry for the formatting, only on mobile.
7
u/tenmilez Aug 10 '21
while (number >= 0) {if (number == 0) return true;if (number == 1) return false;number = number - 2;}edit: apparently I can't code block./shrug.
1
u/nullint Aug 11 '21
Maybe the person was learning
3
u/sktr-guys Aug 10 '21
why u dont check number is not even then return false?
2
u/namnbyte Aug 10 '21
Since the user is looking for even numbers, why not just create a function to divide input by 2, then check if it's a whole number? Less than 3 lines of code
2
u/[deleted] Aug 21 '21
Personally I’d have condensed it to a single if or or or or statement like:
def IsEven(number) { if(number == (2 || 4 || 6 || 8 || 10 || 12 …)) { return true; } else { return false; } }
Much shorter. Less typing.
2
u/Aperture_Executive2 Aug 25 '21
Literally n%2
1
u/Amaragance Aug 10 '21
Yeah, use copy paste for each statement :)
1
u/circlebust Aug 10 '21
The author devised an ingenious, automated "thinking like a programmer" solution: he simply wrote a script that enumerated and then spat out code of all cases for numbers up to the largest prime humans ever used.
1
u/[deleted] Aug 10 '21
You forgot a case in your match block...
1
u/[deleted] Aug 11 '21
Omg, you found the source for modulo /S
1
u/gronbuske Aug 16 '21
Yes, everyone knows you need regex to solve problems like these.
1
u/Aggravating_Ad_6628 Oct 16 '23
im java user
0
u/BeauteousMaximus [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Aug 10 '21
0
u/[deleted] Aug 10 '21
[deleted] — view deleted comment