How does QR code work ?

A QR code (Quick Response code) is a well common type of barcode used to contain data. Nowadays it becomes popular to send data to a person throught QRcode, it is a common advertising strategy since it simplifies the way to access to any data. It has many used such as payment, website login, restaurant ordering, joining a Wi-Fi netc etc. To give an example, it has been by many restaurants to display the menu, order the food, pay, and receive the invoice. It was particulary adapted for the pandemic period in 2020. Almost any smartphone in 2021 can scan the QR code to read and analyze the data behind that bar code. How come the smartphone managed to analyze it so quickly and in less than 5 seconds gives you the message behind that code ? How does it exactly work ?
This article will explain the core functionning of a QR code.

1/ QR code overview

A QR code is always composed of 3 large squares located on the top left, top right and bottom left and a small square in the lower right corner. I have divided the QR code in 3 sections, you have red section, blue section and no color section. The red section is used for alignment, this alignment is used to know how the QR code must be oriented.

The blue section is composed of 2 sets of 15 bits, known as format info. Only one set is needed for the decoding but 2 is safer, and it is used as a backup. About those sets of bits, the bits from 00 to 09 are insignificant and can be ignored, only the bits from 10 to 14 is interesting because it gives us the format marker.

white = 0
black = 1
The format marker is written from the bit 14 to the bit 10, in our case format marker = 11111 The format marker has been XOR with 10101. So let's XOR again with 10101 the retrieve the real value of format marker.
11111 XOR 10101 = 01010
The two first bits represent the error correction level, and the 3 following bits (010) indicates the type of mask used by the QR code. Let's apply the mask (010) on the data section of the QR code.
QR Code XOR Mask 010
XOR table:
0 0 1
0 1 0
1 0 0
1 1 1
It will reverse the color of the QR code when j%3 != 0 After XORed the QR code with the mask that we have identified, we have to read the data from the bottom right to the top left in a zig-zag pattern. From the bottom right corner, we have 0100 indicating the type of QR code.
0001 : numeric (10 bits)
0010 : alphanumeric (9 bits)
0100 : byte (8 bits)
Our QR code is a byte type so the length field is 8 bits long. We continue reading the data in a zig-zag way. The next 8 bits indicates how much bytes our QR code can handle. In our case, the next 8 bits are 00000010 so our QR code can handle at the maximum 2 bytes. Now we can decode the data by translating binary to ascii to see what kind of message we do have behind that QR code.