7 years ago

+ and - nothing more


A little nice thing to know is that computers are basically not capable of doing much math. All they can do is add and subtract and by clever tricks we create the illusion they can do more, and therefore solve nearly all mathematical problems.

Now this sounds odd, doesn’t it? After all I bet all people who’ve been working with programming languages have also worked with multiplications, dividing, and maybe even functions like sine and cosine, etc. All fake.

But how does it actually work. Well, I’ll demonstrate this with a kind of a C like syntax.

Imagine this formula:

		
			7 * 3
		
	

Well, everybody who knows their multiplication tables know that the outcome is 21, but that is now not relevant. How does the computer calculate this?

Like THIS:

		
			int result = 0;
for(int i=0; i<7; ++i) result = result + 3;
		
	

Are you for real, Jeroen? Yes, I am! That is really the true way a computer calculates 7 * 3, and trust me, the outcome will be the same.

7 * 3 = 21
3 + 3 + 3 + 3 + 3 + 3 + 3 = 21
Or to spell keep track of it:
3(3) + 3(6) + 3(9) + 3(12) + 3(15) + 3(18) + 3(21)

This should basically suggest that 37 might take less time to calculate than 73, but I am not sure if this is entirely true, and I bet developers of compilers already took these kinds of things into account.

But how does this go with dividing

		
			21 / 7
		
	

Easy:

		
			int result=0;
for(int i=21;i>0;i = i - 7) result++;
		
	

Trust me the number in the variable “result” will be 3, which is the correct outcome to 21:7.

Now lastly I’ll explain how a modulo is calculated. Let’s just tell in with this C++ example:

		
			   int modulo;
   for(modulo=25;modulo-7>=0;modulo = modulo - 7);
   cout << modulo << "\n";

   // Just to check to see this is true :P Outcome will be the same ;)
   cout << 25 % 7 << "\n";
		
	

Of course, those who are well versed in assembly could argue with me since multiply and divide commands do exist there. True. In assembly they can be used to make complex calculations shorter to write out, however in assembly the shortest way is not always the fastest, and these commands will in the end in pure machine language either directly or in a longer way around come down to the process I demonstrated above in C/C++.

And this will also show how this program in Brainfuck (yeah, that’s really the name of that language, I can’t help it), can work

		
			===================================================================
======================== OUTPUT STRING ============================
===================================================================
>++++++++[<++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++.[-]
>++++++++++[<++++++++++>-]<+++++++++.[-]
>++++++++++[<++++++++++>-]<+.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]
>++++++++++[<++++++++++>-]<++++++++++++++++.[-]
>++++++++++[<++++++++++>-]<+++++++++++.[-]
>+++++++[<+++++++>-]<+++++++++.[-]
>+++++[<+++++>-]<+++++++.[-]

===================================================================
======================== INPUT NUMBER  ============================
===================================================================
+                          cont=1
[
 -                         cont=0
 >,
 ======SUB10======
 ----------

 [                         not 10
  <+>                      cont=1
  =====SUB38======
  ----------
  ----------
  ----------
  --------

  >
  =====MUL10=======
  [>+>+<<-]>>[<<+>>-]<     dup

  >>>+++++++++
  [
   <<<
   [>+>+<<-]>>[<<+>>-]<    dup
   [<<+>>-]
   >>-
  ]
  <<<[-]<
  ======RMOVE1======
  <
  [>+<-]
 ]
 <
]
>>[<<+>>-]<<

===================================================================
======================= PROCESS NUMBER  ===========================
===================================================================

==== ==== ==== ====
numd numu teid teiu
==== ==== ==== ====

>+<-
[
 >+
 ======DUP======
 [>+>+<<-]>>[<<+>>-]<

 >+<--

 >>>>>>>>+<<<<<<<<   isprime=1

 [
  >+

  <-

  =====DUP3=====
  <[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<

  =====DUP2=====
  >[>>+>+<<<-]>>>[<<<+>>>-]<<< <

  >>>

  ====DIVIDES=======
  [>+>+<<-]>>[<<+>>-]<   DUP i=div

  <<
  [
    >>>>>+               bool=1
    <<<
    [>+>+<<-]>>[<<+>>-]< DUP
    [>>[-]<<-]           IF i THEN bool=0
    >>
    [                    IF i=0
      <<<<
      [>+>+<<-]>>[<<+>>-]< i=div
      >>>
      -                  bool=0
    ]
    <<<
    -                    DEC i
    <<
    -
  ]

  +>>[<<[-]>>-]<<          
  >[-]<                  CLR div
  =====END DIVIDES====

  [>>>>>>[-]<<<<<<-]     if divides then isprime=0

  <<

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

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

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

  >>

  ===================================================================
  ======================== OUTPUT NUMBER  ===========================
  ===================================================================
  [>+<-]>

  [
   ======DUP======
   [>+>+<<-]>>[<<+>>-]<

   ======MOD10====
   >+++++++++<
   [
    >>>+<<              bool= 1
    [>+>[-]<<-]         bool= ten==0
    >[<+>-]             ten = tmp
    >[<<++++++++++>>-]  if ten=0 ten=10
    <<-                 dec ten     
    <-                  dec num
   ]
   +++++++++            num=9
   >[<->-]<             dec num by ten

   =======RROT======
      [>+<-]
   <  [>+<-]
   <  [>+<-]
   >>>[<<<+>>>-]
   <

   =======DIV10========
   >+++++++++<
   [
    >>>+<<                bool= 1
    [>+>[-]<<-]           bool= ten==0
    >[<+>-]               ten = tmp
    >[<<++++++++++>>>+<-] if ten=0 ten=10  inc div
    <<-                   dec ten     
    <-                    dec num
   ]
   >>>>[<<<<+>>>>-]<<<<   copy div to num
   >[-]<                  clear ten

   =======INC1=========
   <+>
  ]

  <
  [
   =======MOVER=========
   [>+<-]

   =======ADD48========
   +++++++[<+++++++>-]<->

   =======PUTC=======
   <.[-]>

   ======MOVEL2========
   >[<<+>>-]<

   <-
  ]

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

  ===================================================================
  =========================== END FOR ===============================
  ===================================================================

  >>>>>>>
 ]
 <<<<<<<<

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

======LF========

++++++++++.[-]
		
	

Brainfuck only processes 8 characters and they are full commands:
+ (add)
- (subtract)
> (next memory address)
< (previous memory address)
. (print current char)
, (input current char through keyboard)
[ (start loop as long as char is higher than 0. A while-loop)
] (end of the loop)
All other characters are ignored, and could therefore be used to comment the source.

What it does? It calculates prime numbers from 2 until a number the user enters. (Disclaimer: I did not write that BF code myself, and honestly I don’t know who did). Only 2 commands perform actual calculation. + and -, so it can only add an subtract. How can it do something like calculating prime numbers. Now I do have a Brainfuck compiler (for DOS) so I could check this out, and yeah, the code works….

Crazy, huh?



5 comments

Loading...

Next up

Killing only brings you so far!

Game redo on Cynthia Johnson

Can you see why this pyramid deal could never be solved from the start (regardless what the already removed cards are)?

A few facts and fables about fire.

Disney Princesses go to Hogwarts

Why do people wanna be a #moderator? Is it really such a #cooljob?

Celebrities (almost) killed by one of their biggest fans

A #phrase of #wisdom from the French scientist and philosopher #BlaisePascal Two stories, the one tied to this game (new version), and a prequel novel based on this game have this quote. Perhaps you understand why....

BallPlay future

Current situation on TFT REVAMPED