Arithmetic Shot

I have a program that I am trying to decrypt. It translates to C from another language ( whose name is not listed here ), and since I want to understand how it works, I slowly rewrite the code and simplify it to use all the useful C logical constructs that can offer.

The following small bit with variable values โ€‹โ€‹of X and Y continues to appear in my code:

 ptr[X]--; while(ptr[X]) { ptr[X]--; ptr += Y; } 

ptr is of type char * , and I cannot really make assumptions about the state of the array at any point, because it is pretty deeply embedded in loops and depends on input and output. I can successfully simplify to:

 for(ptr[X]--; ptr[X]; ptr[X]--, ptr += Y); 

But this is just awful. With such a little bit better:

 for(ptr[X]--; ptr[X]; ptr += Y) ptr[X]--; 

I want to know if anyone can come up with a better simplification of the above code, I would really appreciate it. This happens in at least five places, and it impairs my ability to simplify and understand flow control, so if someone can provide a more understandable / readable version, it will be awesome. If someone could just offer some bizarre understanding of this code, that would be awesome as well, although I basically understand what it does.

Understanding the code for a particular X and / or Y may also help. Y tends to be between -2 and 2, and X usually 1, which is why.

+4
source share
4 answers

ptr[X] equivalent to *(ptr + X) , so we can rewrite it as follows:

 for((*(ptr + X))--; *(ptr + X); (*(ptr + X))--, ptr += Y); 

Now there is a lot of redundancy here, so we can simplify this:

 char *ptr_plus_x = ptr + X; for((*ptr_plus_x)--; *ptr_plus_x; (*ptr_plus_x)--, ptr_plus_x += Y); 

Then we can completely get rid of ptr_plus_x :

 ptr += X; for((*ptr)--; *ptr; (*ptr)--, ptr += Y); 

In English, we visit memory cells at offsets X, X + Y, X + 2Y, X + 3Y, ..., decreasing each memory cell until we find a memory cell that is 0. But the test for 0 always happens after decrement , so we are really looking for the first memory location in this sequence with a value of 1. As soon as we find this, we will reduce it to 0 and stop.

If Y is 1, then we reduce the line of consecutive memory locations going forward, up to and including the first. If Y is -1, the same thing happens, but a search is performed backward from the offset X. If Y is 0, an infinite loop occurs. If Y is any other value, the search pattern skips the various entries.

This is not a very intuitive feature, so I can understand why you are confused.

+8
source

I will write:

 ptr[X]-- while (ptr[X]--) ptr+=Y; 

first evaluate, then reduce (provided that there is)

Edit: Alright, I will hate myself in the morning. Ready at this level, right?

 dec: ptr[x]-- while (ptr[X]){ ptr+=Y; goto dec; } 

(I honestly don't know whether to leave it or not.)

EDIT2: so how about this? (tcc did not complain)

  while (ptr[X]--?ptr[X]--,ptr+=Y:0){} 

EDIT 2 1/2;

  //longshot while (ptr[X]--?ptr[X]--,ptr+=Y, ptr[X]:0){} 

If all else fails.

EDIT3: the latest for today.

 while (ptr[X]--?ptr[X]--,ptr+=Y:0){ if (!ptr[X]) break; }//good luck with this, it has been very amusing. 
+3
source

A website for him that should not be named:

 The semantics of the it-which-shall-not-be-named states commands can also be succinctly expressed in terms of C, as follows (assuming that p has been previously defined as a char*): > becomes ++p; < becomes --p; + becomes ++*p; - becomes --*p; . becomes putchar(*p); , becomes *p = getchar(); [ becomes while (*p) { ] becomes } 

So, it seems pretty easy to convert it to C.

EDIT: Here is Hello World BF converted to C ++.

+2
source

It is pretty simple, as it is, already. Instead of writing fewer statements, I would rather try to understand the intent and add a comment.

Example value of fragment โ€œaโ€: reduce all elements of a column (X) of a matrix from Y columns. You will need to draw a vertical line + ses, for example, in a language that does not have a direct purpose.

You can clarify this value by specifying indexes directly:

 // set elements of column to cGoal for( int decrementsToGoal = cGoal; decrementsToGoal != 0; --decrementsToGoal ) { // decrease all elements of column X for( int row = cMaxRows; M[ row*matrixsizeY + columnX ]; --row ) { --M[ row*matrixsizeY + columnX ]; } } 

Good luck :)

0
source

All Articles