Some side projects using AI

  • Zurich First-Hand: News and events in Zurich.
  • The Long Run: New research on aging and healthy living. Refreshed every night.
  • Zug um Zug: Schachnachrichten aus der Schweiz – Turnierberichte, Partieanalysen und Termine. Refreshed nightly.
  • Some Chess Olympiad 2026 games analysed: I used stockfish and Claude to analyse the games of Uzbekistan, Chian, India, USA, Germany, Switzerland, and Greece.

All of them are using extensively AI.

The capabilities of AI are improving a lot quite fast. I have written some thoughts about that a year ago.

Static member variables in C++

The static keyword in C++ has many uses in variables and function. Today, I needed a variable which would store the previous value of another variable among executions of a particular function (or method if you prefer since I was using classes). It should not be affected by calls of other objects on the same function. My first thought was the use a static variable, which was a mistake in this context since there is a single instance of a static member variable among different objects of the same class, no matter the scope of the variable (i.e where you declared that particular variable as static). Thankfully, before implementing it, I tested it since C++ is enormous enough to confuse you. My error was obvious after a few lines of code.

// File: Cl.h
#ifndef CL_H
#define CL_H


class Cl
{
    public:
        Cl();
        const static int static_class_variable = 0;
        int member;
        void method(int, bool);
};

#endif
// File: Cl.cpp
#include <iostream>
#include "Cl.h"

Cl::Cl()
{
    member = 1;
}

void Cl::method(int j, bool setme)
{
    static int local_static = 0;
    if (setme) {
        local_static = j;
    }


    std::cout << "static_class_variable is: " << static_class_variable << std::endl;
    std::cout << "local static is: " << local_static << std::endl;
}


int main() {
    Cl obj;


    obj.method(10, false); // static_class_variable is: 0, local static is: 0
    obj.method(10, true); // static_class_variable is: 0, local static is: 10    


    Cl obj2;    


    obj2.method(10, false); // static_class_variable is: 0, local static is: 10
    //obj2.method(10, true);    


    return 0;
}

If anyone wonders how to retain the previous value of a member variable, just use another one.

Encrypted messages (just for fun)

There is no real encryption. It is just for fun. The following program outputs an integer in decimal which has to be “decrypted” to hex and then to ascii in order to display the intended message.


#!/usr/bin/env python3

if __name__ == '__main__':
    a = input('Insert your string please: ')
    hex1 = 0
    for c in a:
        hex1 = 16**2 * hex1 + ord(c)
    secret_message = ''.join(['echo ', str(hex1), 'P | dc' ])
    print(secret_message)

We can display easily the message using a program like dc(1) — an arbitrary precision calculator. The aforementioned program outputs how we can do it for convenience. 

 

Replacing modulo

Let’s build a counter like a clock. We want to start from 0 until 23 and then back to 0.

A first try could be:


def hour():
    i = 0
    while True:
        i += 1
        yield i % 24

def main():
    for i in hour():
        print(i)

if __name__ == '__main__':
    main()
 

Suppose we have already profiled our program and this is its bottleneck. We know that modulo is an expensive operation sometimes. That is why we strive for its replacement by other more elementary operations.

If the hours were a power of 2 (like 16 or 32), we could employ a trick using binary operations which are very efficient since the all numbers are internally represented in the binary system and a computer is so “familiar” with those operations that he can execute them really fast. The following code demonstrates this trick were we suppose that the number of hours in a day is 32 (we wish!).


def hour():
    i = 0
    while True:
        i += 1
        yield i & 31

def main():
    for i in hour():
        print(i)

if __name__ == '__main__':
    main()

Notice that the trick does not work for values which are not powers of 2. Why does it work in these cases? All the magic is due to the & operator which denotes the and operation. The and operation takes as input two binary digits (0 or 1) and outputs a binary digit as follows: 0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 0. It is easy to observe that it always outputs 0 unless both its inputs are 1. There are two key observations here.

  1. If an input is always 1, then and outputs the contents of its other input.
  2. If an input is always 0, then and outputs always 0.

In the decimal system, every digit has a weight which is a power of 10. For instance the number 1234 = 1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0. Similarly, in the binary system every digit has a weight which is a power of 2. 0101 = 0 * 2^3 + 1 * 2^2 + 0 * 2 ^1 + 0 * 2^0 = 5. As we would expect, as a power 10 in the decimal system is an 1 followed by as many zeros as the exponent of that power, the same holds for the binary system. So, 2^5 = 100000 = 32. Now, substracting 1 by 32 would leave us with: 31 = 011111 = 31.

When the Python interpreter executes the statement `i & 31` imagine a procedure like the following:

  1. Find the value of i (it already is in binary form).
  2. Write the number 31 in binary form.
  3. Pad (from the left) with extra zeros if need be.
  4. Execute the and operation

Every digit of i is unaltered by this operation except the highest order digits, since the 5 least significant digits of 31 are all 1. On the other hand, the higher order digits are 0, so the generator hour() can not yield a number larger than 31. Since after 31 is the number 32 which is represented as (100000) and then it is masked (the technical term when we apply the operation and to a number with another known number) with (011111) the result is always (000000). So, the counter start over again.

But what if we would like a more general approach even for numbers which are not a power of 2? Then, we could use a more general approach like:


from math import log
from math import ceil

def almostmodulo(x, y):
    higher_power = 2 ** ceil(log(y, 2)) - 1
    remains = higher_power - y + 1
    if ((x + remains) & higher_power == 0):
        return 0
    else:
        return x + 1
 

The operation of finding the higher power of 2 is relatively simply using a binary operation called shift and is really needed only once throughout the execution of the program. We see that we use the same trick, but use the variable remains to cover the difference with the higher power of two. The catch here is that the control flow costs too much and that is why we do not see this code in practice. We could have removed it if we knew the variable remains contains the number 1 (find out how. ) but then we fall back to the previous case! Be aware that the last code does not work when y is a power of 2, ie, in the cases that the second one works!

So, what is the meaning of this post? Which technique should we use to write super fast code? Well, the answer is trivial. Use your profiler firstly and then decide what you should optimize. Then, if it comes down to such a detail, you should probably consider writing the whole thing in another language like C. The purpose of this post was to demonstrate a few tricks and to give a clue how could the internal representation of a program and its data which are depended on the computer architecture could affect the decision of a programmer. Even though most likely the aforementioned programmer would not be one who uses a high level language, but someone who writes compilers.

Descriptive Essays and Software development

A descriptive essay is the way of portraying a situation or an physical object (or subject) through the use of a natural language (such as English). It makes use of sensory details and it is usually structured spatially.

 

Software development is the development of a software designed for a purpose through computer programming. It makes use of the logical representation of ideas and there are several ways to structure it.

Some suggested steps for the development of an essay:

  1. Determine the topic and its scope.
  2. Brainstorm some ideas approaching the topic.
  3. Evaluate these ideas based on criteria like relevance, ability to explain it, ways of describing it etc. Use a cutoff threshold to reject not so good ideas. 
  4. Put an order of presentation of these ideas.
  5. Specify the details of the ideas that are going to be used.
  6. Write the introduction.
  7. Write the body paragraphs.
  8. Write your conclusion.
  9. Refine iteratively.

Some ideas to help you through these steps:

  1. Think about what you could write.
  2. Think about why you should write about something.
  3. Have you written enough? Is there anything missing?
  4. Rinse and repeat.

Some suggested steps for the software development: Look above.