How create an Little Mans Computer (LMC) code that will get a number. Display 1 if the number is odd, display 0 if the number is even

后端 未结 3 1541
夕颜
夕颜 2021-01-29 15:19

I need help in my study\'s where it can display 1 if the number is even and 0 if the number is odd. for example if the input is 99 it will display the output 1 which means odd.

3条回答
  •  伪装坚强ぢ
    2021-01-29 16:02

    wow, that thing does not have AND?
    LMC link 1
    LMC wiki

    Hm, ok, let me think for a while, without AND this is tricky. On algorithm level it looks like you have to loop trough decreasing the input number by one till zero, and do result = 1 - result each iteration. At the end you will have either one or zero in result (depending on the initial value of result (0 or 1, you decide), and oddness of the input number).

    Now you have to write this for LMC, I'm not going to learn it on the fly now. If you are completely lost, read the wiki page, find out some emulator, run in it some example code, and study it instruction by instruction, what it does, try to figure out why. Then try few more examples and see how they work, then come back to the algorithm I wrote, and try to write it on your own.

    (if still lost, you should have a specific question to each of those points, which will be much easier to answer, some of them very suitable for SO .. your original question looks a bit too broad and lacking, I sort of wonder you haven't been already downvoted and put "on hold").

    edit (for fun):
    That r=r-1 is a way how to flip between 0 and 1 when you have only subtraction. Usually CPUs have also xor and not instructions making the operation more straightforward (from bitwise assembler programmer point of view), but I tend to use the r=r-1 in Java to make it easier for reading for my colleagues, who keep forgetting what is xor and then they are puzzled by r^=1;

提交回复
热议问题