How do I use mathematical operators like modulo in Brainfuck?












0














In BF, I know you can add and subtract with the + and - operators, and that you can multiply two pre-defined numbers like so:



Multiply 4 and 4
++++[>++++<-]


But how can I multiply two variables, or use other mathematical operators, such as modulo or division?










share|improve this question
























  • multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
    – Pedro del Sol
    Apr 24 '15 at 14:17










  • @PedrodelSol Could you leave an answer on how I would implement those?
    – ASCIIThenANSI
    Apr 24 '15 at 14:19










  • Multiplication using increments
    – phuclv
    Apr 25 '15 at 5:40
















0














In BF, I know you can add and subtract with the + and - operators, and that you can multiply two pre-defined numbers like so:



Multiply 4 and 4
++++[>++++<-]


But how can I multiply two variables, or use other mathematical operators, such as modulo or division?










share|improve this question
























  • multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
    – Pedro del Sol
    Apr 24 '15 at 14:17










  • @PedrodelSol Could you leave an answer on how I would implement those?
    – ASCIIThenANSI
    Apr 24 '15 at 14:19










  • Multiplication using increments
    – phuclv
    Apr 25 '15 at 5:40














0












0








0







In BF, I know you can add and subtract with the + and - operators, and that you can multiply two pre-defined numbers like so:



Multiply 4 and 4
++++[>++++<-]


But how can I multiply two variables, or use other mathematical operators, such as modulo or division?










share|improve this question















In BF, I know you can add and subtract with the + and - operators, and that you can multiply two pre-defined numbers like so:



Multiply 4 and 4
++++[>++++<-]


But how can I multiply two variables, or use other mathematical operators, such as modulo or division?







math brainfuck






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 18:39









Zoe

11k73675




11k73675










asked Apr 24 '15 at 14:08









ASCIIThenANSI

672623




672623












  • multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
    – Pedro del Sol
    Apr 24 '15 at 14:17










  • @PedrodelSol Could you leave an answer on how I would implement those?
    – ASCIIThenANSI
    Apr 24 '15 at 14:19










  • Multiplication using increments
    – phuclv
    Apr 25 '15 at 5:40


















  • multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
    – Pedro del Sol
    Apr 24 '15 at 14:17










  • @PedrodelSol Could you leave an answer on how I would implement those?
    – ASCIIThenANSI
    Apr 24 '15 at 14:19










  • Multiplication using increments
    – phuclv
    Apr 25 '15 at 5:40
















multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
– Pedro del Sol
Apr 24 '15 at 14:17




multiplication is just repeated addition; 4 * 4 == 4 + 4 + 4 + 4. similar for division/subtraction. exponentiation and modulo can also be expressed using addition and subtraction.
– Pedro del Sol
Apr 24 '15 at 14:17












@PedrodelSol Could you leave an answer on how I would implement those?
– ASCIIThenANSI
Apr 24 '15 at 14:19




@PedrodelSol Could you leave an answer on how I would implement those?
– ASCIIThenANSI
Apr 24 '15 at 14:19












Multiplication using increments
– phuclv
Apr 25 '15 at 5:40




Multiplication using increments
– phuclv
Apr 25 '15 at 5:40












2 Answers
2






active

oldest

votes


















5














Here is a generic looking routine to multiply two numbers in bf. Taken from here.



Multiply a value by another value
This requires multiple copies and nested loops.



Copy first operand to a work location
Move to work location and enter a loop
Move to second operand and copy to a work location
Move to second work location and enter a loop
Move to result location and increment
Move to second work location and decrement
Loop until second work location equals zero
Move to first work location and decrement
Loop until first work location equals zero


Example:



Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].



+++>+++++< 
[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
[<<
[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
[<<+>>-]
<-]


Some further reading here






share|improve this answer





























    1














    Refer to this link:



    https://esolangs.org/wiki/Brainfuck_algorithms



    it uses values such as temp0, x, y, etc. You should pick cells to hold these values, and replace the variable names with > or < to go to the cell holding it






    share|improve this answer

















    • 1




      While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
      – Patrick Hund
      Nov 23 at 15:48











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29849673%2fhow-do-i-use-mathematical-operators-like-modulo-in-brainfuck%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    Here is a generic looking routine to multiply two numbers in bf. Taken from here.



    Multiply a value by another value
    This requires multiple copies and nested loops.



    Copy first operand to a work location
    Move to work location and enter a loop
    Move to second operand and copy to a work location
    Move to second work location and enter a loop
    Move to result location and increment
    Move to second work location and decrement
    Loop until second work location equals zero
    Move to first work location and decrement
    Loop until first work location equals zero


    Example:



    Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].



    +++>+++++< 
    [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
    [<<
    [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
    [<<+>>-]
    <-]


    Some further reading here






    share|improve this answer


























      5














      Here is a generic looking routine to multiply two numbers in bf. Taken from here.



      Multiply a value by another value
      This requires multiple copies and nested loops.



      Copy first operand to a work location
      Move to work location and enter a loop
      Move to second operand and copy to a work location
      Move to second work location and enter a loop
      Move to result location and increment
      Move to second work location and decrement
      Loop until second work location equals zero
      Move to first work location and decrement
      Loop until first work location equals zero


      Example:



      Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].



      +++>+++++< 
      [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
      [<<
      [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
      [<<+>>-]
      <-]


      Some further reading here






      share|improve this answer
























        5












        5








        5






        Here is a generic looking routine to multiply two numbers in bf. Taken from here.



        Multiply a value by another value
        This requires multiple copies and nested loops.



        Copy first operand to a work location
        Move to work location and enter a loop
        Move to second operand and copy to a work location
        Move to second work location and enter a loop
        Move to result location and increment
        Move to second work location and decrement
        Loop until second work location equals zero
        Move to first work location and decrement
        Loop until first work location equals zero


        Example:



        Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].



        +++>+++++< 
        [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
        [<<
        [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
        [<<+>>-]
        <-]


        Some further reading here






        share|improve this answer












        Here is a generic looking routine to multiply two numbers in bf. Taken from here.



        Multiply a value by another value
        This requires multiple copies and nested loops.



        Copy first operand to a work location
        Move to work location and enter a loop
        Move to second operand and copy to a work location
        Move to second work location and enter a loop
        Move to result location and increment
        Move to second work location and decrement
        Loop until second work location equals zero
        Move to first work location and decrement
        Loop until first work location equals zero


        Example:



        Multiply three (a[p]) times five (a[p+1]) and place the result in a[p+2].



        +++>+++++< 
        [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
        [<<
        [>>>+>+<<<<-]>>>>[<<<<+>>>>-]<
        [<<+>>-]
        <-]


        Some further reading here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 24 '15 at 14:38









        Pedro del Sol

        2,29483340




        2,29483340

























            1














            Refer to this link:



            https://esolangs.org/wiki/Brainfuck_algorithms



            it uses values such as temp0, x, y, etc. You should pick cells to hold these values, and replace the variable names with > or < to go to the cell holding it






            share|improve this answer

















            • 1




              While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
              – Patrick Hund
              Nov 23 at 15:48
















            1














            Refer to this link:



            https://esolangs.org/wiki/Brainfuck_algorithms



            it uses values such as temp0, x, y, etc. You should pick cells to hold these values, and replace the variable names with > or < to go to the cell holding it






            share|improve this answer

















            • 1




              While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
              – Patrick Hund
              Nov 23 at 15:48














            1












            1








            1






            Refer to this link:



            https://esolangs.org/wiki/Brainfuck_algorithms



            it uses values such as temp0, x, y, etc. You should pick cells to hold these values, and replace the variable names with > or < to go to the cell holding it






            share|improve this answer












            Refer to this link:



            https://esolangs.org/wiki/Brainfuck_algorithms



            it uses values such as temp0, x, y, etc. You should pick cells to hold these values, and replace the variable names with > or < to go to the cell holding it







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 25 '15 at 5:32









            Cedric Mamo

            1,15211229




            1,15211229








            • 1




              While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
              – Patrick Hund
              Nov 23 at 15:48














            • 1




              While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
              – Patrick Hund
              Nov 23 at 15:48








            1




            1




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Patrick Hund
            Nov 23 at 15:48




            While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
            – Patrick Hund
            Nov 23 at 15:48


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29849673%2fhow-do-i-use-mathematical-operators-like-modulo-in-brainfuck%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Trompette piccolo

            Slow SSRS Report in dynamic grouping and multiple parameters

            Simon Yates (cyclisme)