<   Back to PortfolioPortfolio.html

A Code Poem

I wrote this poem (called ‘1701998444’) when I first knew about the Code Poems project by my friend Ishac Bertran. It is dedicated to a girl named Lorenza.

First of all, what is a code poem? With Ishac’s words:

  1. Poetry is considered a form of literary art in which language is used for its aesthetic and evocative qualities. It contains multiple interpretations and therefore resonates differently in each reader.

  2. Code is the language used to communicate with computers. It has its own rules (syntax) and meaning (semantics). Like literature writers or poets, coders also have their own style that include - strategies for optimizing the code being read by a computer, and facilitating its understanding through visual organization and comments for other coders.

  3. Code can speak literature, logic, maths. It contains different layers of abstraction and it links them to the physical world of processors and memory chips. All these resources can contribute in expanding the boundaries of contemporary poetry by using code as a new language. Code to speak about life or death, love or hate. Code meant to be read, not run.

Let’s get to the poem.

  1. #include <stdio.h>

  2. long int n = 1701998444;

  3. int main() {

  4.     int i = 0;

  5.     while( i < n ) {

  6.         putc( ((n >> (i % 4) * 8) & 0xff)

  7.             + 4 * ((int) (i % 8 % 7) / 6),

  8.             stdout);

  9.         i++;

  10.     }


  12.     return n;

  13. }

So why do I think this can be considered poetry? I think there are at least three reasons.

1. A letter is a number and a number is a letter

You may wonder what the number 1701998444 means. The computer stores this number in memory as 32 bits (either 0 or 1), equivalent to 4 bytes:

0110 0101   0111 0010   0110 1111   0110 1100

These four bytes can be read separately as numbers:

0110 0101 equivalent to 101

0111 0010 equivalent to 114

0110 1111 equivalent to 111

0110 1100 equivalent to 108

If you have ever heard of the ASCII code then you know those numbers can be read as letters, in particular:

0110 0101 equivalent to e

0111 0010 equivalent to r

0110 1111 equivalent to o

0110 1100 equivalent to l

This means the number 1701998444 can be read as the word ‘erol’, which happens to be the reverse of ‘lore’.

This piece of code is actually going through the four bytes of 1701998444 and printing them on screen:

  1. putc( (n >> (i % 4) * 8) & 0xff), stdout);

In this line of code I am assuming that 4 is the number of bytes in an unsigned int. That’s actually what sizeof(unsigned int) returns on most machines.

8 is the number of bits in a byte and it makes the shifting operator >> work byte per byte.

Since i starts from zero and gets increased, this will print ‘lore’ (not ‘erol’).

2. No need for an “if”

One idea that I had was to alternate the word ‘lore’ with the word ‘love’.  To do this programmers would normally use an if statement. Something like that:

if(condition) putc(114, stdout) // 114 is ‘r’

else putc(118, stdout)          // 118 is ‘v’

I really wanted to avoid an if statement, to make the poem more obscure but also to keep the print statement as simple as possible.

...Did you know you can actually use math to avoid an if statement? I didn’t! Yet I found out writing this poem. This is the piece of code that makes everything work:

+ 4 * ((int) (i % 8 % 7) / 6)

In order to print ‘lorelove’ what needs to change is the third letter every two words, which is equivalent to the seventh letter every 8 characters.

As you can see from the code, i goes from 0 to n. i % 8 makes i a number from 0 to 7. I am doing this because ‘lorelove’ is exactly 8 letters. (i % 8 % 7) makes is then a number from 0 to 6. This way the value will be 6 exactly on the 7th characters (‘lorelove’). Dividing everything by 6 makes it possible to have 0on every character except the 7th —which is 1.

value of i % 8               0 1 2 3 4 5 6 7

value of i % 8 % 7           0 1 2 3 4 5 6 0

value of (i % 8 % 7) / 6     0 0 0 0 0 0 1 0

I am then multiplying by 4 because that’s the difference between ‘r’ (114 in ASCII) and ‘v’ (118 in ASCII).

3. What gets printed to screen

Can you guess what the code will actually do? Look at the result in the video below. Even though what actually appears on the screen is a long sequence of ‘lorelove’, they are printed so fast that it looks like all the ‘lore’ gets intermittently changed into ‘love’. This behavior may change depending on the width of your Terminal window.

Finally, is the sequence infinite? No, the poem prints exactly 1701998444 words.


How do I try this out?

You need to have a C compiler installed on your machine. If you’re on a Mac the easy way is to install XCode. Save the code as ‘lore.c’ on your Desktop. Then open Terminal.app and type

            cd Desktop

            gcc -o lore lore.c


Why is this so cryptic?

Well, many poems can be very cryptic. The awesome thing about code poems is that you can get an additional level of interpretation once the code is actually run.

You could have optimized the code in this or that way.

Awesome! Email me at hi@marcotriverio.com and we can co-create version 2.0.

I still do not understand part of the code.

Feel free to email me at hi@marcotriverio.com and I’ll be happy to provide a more thorough explanation.

Isn’t this completely useless?

I have felt really inspired by the Code Poems project. I think it is a great exploration in the mix between technology and art. I do not know whether my code is actually poetic or not, but I enjoyed so much thinking of code as a form of art. Removing the need for an ‘if’ statement? That was cool to find out!