Our Fathers’ Faults – Failure is not an Option

Our Fathers’ faults.

Intelligent people learn from their mistakes, wise people learn from other’s mistake. Unlucky people learn from the mess they have to fix in someone else’s code.

Working as a programmer is not always an easy task. On lucky days you feel like a god planning and designing architectures, wisely crafting virtual nuts, cogs, and bolts that happily tick together in an elegant and coordinated choreography, while on bad days it is bug fixing in your code. In really sh**ty days it is bug hunting in code written (by a long-gone someone else) for exploration, demo, and test of the coolness factor.

Read moreOur Fathers’ Faults – Failure is not an Option

A Type with Character

So, you know everything about C++ integral types, don’t you?

I thought I did, until when I enabled clang-tidy on my project. It all started with the rather innocent-looking warning:

warning: use of a signed integer operand with a binary bitwise operator

It looked somewhat less innocent when, examining the line, I saw no evidence of signs.

But, let’s start from the comfort zone (at least, my comfort zone):

unsigned x = 32;

The type of ~x (bitwise negation) is still unsigned. No surprise here, really obvious. The diligent programmer finds that the data fits in a smaller integer and writes:

   uint8_t s = 42;

Can you guess the type of ~s? Give it a try, really. Ready? Well, the type of ~s is… int. What? A quick check of other expressions involving uint8_t yields the same … fascinating result. Apparently these expressions are all converted into int.

In other words (and with a bit of syntax bending) uint8_t+uint8_t -> int, uint8_t<<uint8_t -> int, uint8_t+1 -> int. Let me rephrase that, in every expression an uint8_t type is converted to int.

Time for some Internet Duckduckgoing :-).

Back to our uint8_t (that is nothing but an unsigned char in disguise). When a char value, be it signed, unsigned or plain is encountered in an expression (in C++ standard jargon, a prvalue) it is promoted to int pretty much on every common CPU. On exotic architectures, char and int could have the same size, so int could not hold every possible value of char and therefore it is turned into an unsigned. From a strictly rigorous point of view, the signedness of the type of uint8_t in expression is machine-dependent. Keep this in mind if you aim to write really portable code 😉 (*)

You can find a very good (and somewhat frightening) explanation here.

But I’d like to add something beyond the standard technicalities and look at the reasons for why this is like this and what we can take home.

First, it is important to fix that in C (and C++ by extension) the int/unsigned type is mapped to the most efficient word of the target processor. The language provides that, as long as you use int/unsigned (without mixing them) in an expression, you get the most performing code.

Also, the language mandates that an int be at least 16 bits wide and at most the same size of a long.

What if you need to do math on 8bits data on a 32bits architecture? Well, you need assembly code to insert mask operation for cutting away the excess data in order to reach the right result.

So, the C language opts for the best performance turning everything into int, avoiding the extra assembly for masking and let the programmer arrange the expressions with cast here and there if anything different is desired.

Unexpected type promotion, sign change, and performance penalty should be three good reasons to avoid using anything different from int and unsigned in expressions (or long long when needed) and keep intXX_t and uintXX_t for storage.

Note that this applies also to function arguments. It happens quite frequently to read APIs where types for integer arguments are defined as the smallest integer capable of holding the largest value for a given parameter. That may seem a good idea at first since the API embeds in the interface the suggestion to the user for a proper range.

In fact, this has to be balanced against the aforementioned problems and don’t really enforce the constraints for two reasons – first, you can actually pass any integral type and get not even a warning and second, possibly your accepted range is a subset of all the possible value of the type, therefore the user is still required to read the documentation.

Finally, when in doubt, use the compiler 🙂 Finding types of expressions via the compiler could be not the most intuitive task. Below you find the code I used for my tests. Happy type-checking!

/* Linux only, sorry */
#include <iostream>
#include <cstdint>
#include <cxxabi.h>

int main()
    uint8_t a = 42;
    std::cout << abi::__cxa_demangle(typeid( a ).name(), nullptr, nullptr, nullptr) << "\n";
    std::cout << abi::__cxa_demangle(typeid( ~a ).name(), nullptr, nullptr, nullptr) << "\n";
    std::cout << abi::__cxa_demangle(typeid( 42u ).name(), nullptr, nullptr, nullptr) << "\n";
    std::cout << abi::__cxa_demangle(typeid( ~42u ).name(), nullptr, nullptr, nullptr) << "\n";
    return 0;

(*) Also keep in mind, if you strive to write a portable code, that uintXX_t types may not exist for exotic architectures. In fact, the standard mandates that if the target CPU has no 8 bits type then uint8_t be not defined for such target. You should use uint_least8_t (meaning a data type that has at least 8 bits) and uint_fast8_t (a data type that is the most efficient for operations with 8 bits).

Lambda World 2019

Lambda World 2019 Blog Post

The Spanish town of Cadiz lies outward in the ocean, circled by ancient walls, protected by two castles. Grids of alleys, old houses facing at a short distance across narrow streets, narrow balconies enclosed in windowed structures. Warm, light brown rocks and stone. Old, ancient, historical and yet so filled with sun and sparkling energy.
It is just a heck of a journey to get there to attend the best functional programming conference in the world. You start from your ordinary small northern Italy town, get by car to the nearest airport, fly first to Barcellona, hoping that the turmoils for the recent sentence on the separatist leaders cause no problem with your flight exchange. Wait for a while at the airport terminal, eating a sandwich, then you board on another plane that will carry you to Jerez de la Frontera. There you are up to a ride on the train. Eventually, you reach Cadiz, but you still need to walk to your hotel in the historic downtown. “Walk” because at Cadiz core everything is at a walking distance and most of the streets are impractical to any car.

This is my third year in a row that I manage to get my employer to sponsor my attendance at Lambda World Cadiz. I like this conference, I like the eclectic view of the functional-programming-verse that provides and last, but not least, I like the setting.

Read moreLambda World 2019

Scala Italy 2019

Scala Italy is the Italian conference of the Scala Language now to the 7th edition – a time to meet old friends, to make new friends, to talk about programming and to attend high-quality speech about the Scala language.
This year, following the tradition, started in 2018 🙂 the conference lasted two full days.
Conference videos are published in this Vimeo channel.


By a very rough approximation, I counted about one hundred people, organizers included. While headcount went down (last year we were about 150), the average age went up. Still not at C++ conferences level, but the number of baby programmers was quite low. Also, the female percentage was very low (on par with C++ conferences).

Maybe Scala is leaving the phase of enthusiast early adopters (usually young programmers, still in contestation years, with a lot of time, curiosity and energy to invest in something new) and going toward being something too solid and usable to be used for disputing the establishment.
This isn’t too bad and possibly a sign of maturity. Even the content of some talks of the conference seems to confirm this idea.

Read moreScala Italy 2019

it++ 2019

In Italy, June is usually that period of the year when the weather becomes hot and the Italian C++ conference is held in Milan.

That’s why (the conference, not the weather) on Saturday 15th June, my friend and coworker Francesco and I went to the Milan Polytechnic. As for the other conferences I attended, I wrote quite a large volume of notes. Regretfully, as it happened for the other conferences, reorganizing my notes to make some sense of them and present them in an understandable and useful way is out of reach for my homeopathic level of free time. Also, usually you can find online the slides and videos of the talks well before my post. Therefore I decided for the short format I already used a couple of times in the past – a brief overview describing the conference for what you can’t see in the video e a brief summary of the talks I attended to.

Read moreit++ 2019

*m*zn bye bye

One week ago I received an email from the most successful online store owned by Bezos, telling me that since my site didn’t earn any money in the last 365 days, my affiliation program was terminated. Within a week I had to remove all references to their website from my blog.

Here is the text of the communication:

Il tuo account da affiliato verrà chiuso con effetto entro il termine di sette giorni, entro lo stesso termine terminerà anche l’ Accordo Operativo che lo regola.

Nota – Questa comunicazione riguarda solo l’account elencato nella riga dell’oggetto di questa email.

Perché questa comunicazione?
Perché non sono state effettuate vendite tramite link di affiliazione negli ultimi 365 giorni.

Cosa accade ora?
Dovrai terminare l’utilizzo di ogni contenuto e marchio *m*zon e rimuovere immediatamente tutti i link diretti al sito di *m*zon. Riceverai tuttavia il pagamento per ogni commissione ancora da pagare guadagnata precedentemente a questa comunicazione.

Ti invitiamo tuttavia a candidarti nuovamente in futuro visitando la pagina: https://programma-affiliazione.*m*zon.it.

Ti ringraziamo per la partecipazione al Programma di Affiliazione di *m*zon EU.


I sent them two emails, one to the Italian branch and the other to the US headquarters, stating that mine is not an e-commerce website and my affiliation is more a service to my readers to look up books and/or movies if they are interested (in fact is far more than 365 that referral fees sum up to 0). Despite the claim of a reply in less than 24h I got no response.

Basically, I don’t understand why they have to behave like this, the traffic from my website costs them zero. Also, any traffic that would arrive there could be a potential purchase (this is more up to them to be good at marketing on their website). But also it piss me off that I have to remove all links to their website – I had to go through the database of my website (15 years of posts) and manually replace references. And why? Shouldn’t I be free to link anyone I want to link?

So I decided that *m*zn is history, time to look elsewhere. I would like to join the affiliation program of ibs, but I still have some trouble with their affiliation service tradedoubler.

Garbage-Collecting Garbage

Here we go again, another system written in one of the advanced languages that promise to avoid you the burden of memory management, another unavoidable performance issue and yet again another desperate run to move everything out of garbage man hands into a set of dedicated memory pools.

According to Wikipedia, GC (Garbage Collection) was invented back at the dawn of computer science, (ca 1959) for the LISP language. Since its inception, GC made promptly clear that it didn’t play well with time constraints (see Humorous Annedoct in History of Lisp).

Read moreGarbage-Collecting Garbage

Java Magic Trick

One of the (many?) shortcomings of Java is, at least in my humble opinion, the lack of unsigned integers. This may seem a minor problem, but it becomes a real annoyance when you have to deal with bits and bytes.

Consider the right shift. As a good C/C++ programmer you know that right shifting of a signed integer is generally bad. Java defined two bit-wise right shift operators: >> and >>>. The first extends the sign, so that -2 >> 1 evaluates to -1 and -2 >>> 1 evaluates to (-1 & 0x7FFFFFFF), i.e. the left most bit that becomes vacant pending the shift is filled with a 0.

So far, so good. So you may be surprised from the result of the following code:

class Surprise
    public static void main( String[] args )
        int result = -1 >>> 32;
        System.out.println( "surprise: "+result );

I don’t want to spoil the result, so you may want to take some more time to think… Ready with your guess?

Well Suprise.java prints -1. That’s because the right hand side argument of the >>> operator is masked implicitly by 0x1F (i.e. taking values just between 0 and 31 included).

No warning is emitted during compilation, so you are pretty on your own – either you know or you don’t and trouble are ready to … byte you.

What is in a name?

Here now and then, as a programmer, you have a hard time finding a good name for something. That thingy thing that eludes your wit and skill to name things. And that’s bad because it is hard to reason about unnamed entities. Symbols are deeply embedded in our brainwork and the right word has quite a power to drive thoughts efficiently and reliably.

Read moreWhat is in a name?