News

### Obfuscated C++

LAST MONTH'S OBFUSCATED C++

Last month we asked you to predict the output of the following program:

``````#include
#include
#include
#include
struct {
void operator()(int i){cout<><"\n";} }="" c;="" int="" main(){=""> il(2,1);
list::iterator i1(il.begin()),i2(i1);i2++;
back_insert_iterator<> >i3(il);
for(;*i1<20000;i1++,++i2) *i3="*i1+*i2;" for_each(il.begin(),il.end(),c);="" return="" 0;="">``````

This program prints a series of Fibonacci numbers. It does this by creating a two-element list of integers, and two iterators that point to the two elements:

``````ist il(2,1);
list::iterator i1(il.begin()),i2(i1);i2++;``````

(Note that the list and the first iterator have different names; the lower case "ell" in the list name is easy to confuse with the number "one" in the iterator name.) The first iterator (i1) is initialized to point to the first element of the list. By copying i1 and incrementing it, we set i2 to point to the second element of the list. We then create a back_insert_iterator named i3 that uses our list. Recall that a back_insert_iterator is an output iterator that appends values to the end of its container.

The for loop steps through the list, adding the two elements pointed at by i1 and i2 and storing the sum through i3. This has the effect of adding the two elements and appending the result to the end of the list. We then increment i1 and i2 and repeat. When the loop terminates, the list will be a sequence of integers, each of which is the sum of the two preceding numbersÑwhich is the Fibonacci series.

The final for_each applies the function object c to each element of the list. In this case, c's operator() member function simply prints its argument. So the result of running our program is:

``````1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368``````

This Month's Obfuscated C++
Explain the semantics of this function:

``````unsigned f(unsigned g ) {
return (1 - (bool)(g/0x64))*(0x7D0-0x64*(g/0x32))+g;
}``````

Rob Murray is Director of Engineering at the Irvine office of Nuforia, an object-oriented software consulting company based in Houston, TX. He has taught C++ at technical conferences since 1987 and is the author of C++ Strategies and Tactics. He was the founding editor of C++ Report and can be contacted at [email protected].