Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this ❤ code work?

Tags:

c

I stumbled upon this code on Quora.

#include<stdio.h>

main(){
          int           $[]
        ={0x69,       0154,107,
     'e',0x79,0157, 117,'v',0x6a}
     ,_,__;_=__^__;__=_;while(_<(-
      (~(1<<3))+3)){(_==1<<1||_==
       -(~(1<<3))||_==11)?putchar
        (*($+(1>>1))):putchar(*(
          __++ +$)),(_==1>>1||
            _==1<<2||_==(1<<3
              )-1)?putchar
                (' '):1;
                  _++;
                    }
}

The program's output is i like you viji. It's touching, but cryptic. So I formatted it with indent to get a better idea.

main ()
{
  int $[] = { 0x69, 0154, 107,
    'e', 0x79, 0157, 117, 'v', 0x6a
  }
  , _, __;
  _ = __ ^ __;
  __ = _;
  while (_ < (-(~(1 << 3)) + 3))
    {
      (_ == 1 << 1 || _ ==
       -(~(1 << 3)) || _ == 11) ? putchar
(*($ + (1 >> 1))) : putchar (*(__++ + $)), (_ == 1 >> 1 ||
                                _ == 1 << 2 || _ == (1 << 3) - 1) ? putchar
(' ') : 1;
      _++;
    }
}

Now it's not so touching, but still a little cryptic.

So, can anybody explain how this code manages to print i like you viji?

UPDATE:

gave better names to variables $ , _ and __ and expanded ternary operators:

int a[] = { 0x69, 0154, 107, 'e', 0x79, 0157, 117, 'v', 0x6a }, x, y;

x = y ^ y;
y = x;

while (x < (-(~(1 << 3)) + 3))
  {
    if (x == 1 << 1 || x == -(~(1 << 3)) || x == 11)
      putchar (*(a + (1 >> 1)));
    else
      {
        putchar (*(y++ + a));
        if (x == 1 >> 1 || x == 1 << 2 || x == (1 << 3) - 1)
          putchar (' ');
        else
          1;
      }
    x++;
  }
like image 356
Luboš Turek Avatar asked Feb 09 '15 20:02

Luboš Turek


People also ask

What does ❤ mean in texting?

What does Red Heart emoji ❤️ mean? The red heart emoji is used in warm emotional contexts. It can be used to express gratitude, love, happiness, hope, or even flirtatiousness.

What's the difference between ❤ and ♥?

The main difference in appearance relates to whether the elaborated hearts are shown in pink, or red. There's also design choices where some platforms show ❤️ Red Heart with a gloss and ♥️ Heart Suit as a matte heart, while others show every emoji in matte.

What does ❤ 🔥 mean from a girl?

What does ❤️‍🔥 Heart on Fire emoji mean? The Heart on Fire emoji ❤️‍🔥 depicts a flaming heart. It can be used to represent passionate love (“burning love”) or intense liking of something.

What does ♥ mean?

A heart symbol emoji, used in card games for the hearts suit. In emoji form, this is most commonly used for love, affectiction, romance and positive intentions. Sometimes shown in a slightly darker shade of red than ❤️ Red Heart.


1 Answers

Rewriting the code gives:

int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};
int i0, i1;  // Moved to new line instead of using ,

i0 = 0;  // i0 = i1 ^ i1;
i1 = i0;

while (i0 < 12)  // All strange constant like (1<<3) recalculated
{
    if (i0 == 2 || i0 == 9 || i0 == 11)   // "? :" replaced by if 
    {
            putchar(*arr1);
    }
    else
    {
        putchar (*(arr1 + i1));
        ++i1;

        if (i0 == 0 || i0 == 4 || i0 == 7)
        {
            putchar(' ');
        }
    }
    i0++;
}

Step by step description:

1) Reformat the lines, remove unnecessary spaces, insert spaces for readability

main()
{
    int $[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , _, __;
    _ = __^__;
    __ = _;
    while(_ < (-(~(1<<3))+3))
    {
        (_ == 1<<1 || _ == -(~(1<<3)) || _ == 11) ?
                putchar(*($ + (1>>1))) :
                putchar(*(__++ +$)), (_ == 1 >> 1 || _ == 1<<2 || _ == (1<<3)-1) ?
                        putchar(' ') : 1;
        _++;
    }
}

2) Rename variable, i.e $ to arr1, _ to i0 and __ to i1

main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0 = i1^i1;
    i1 = i0;
    while(i0 < (-(~(1<<3))+3))
    {
        (i0==1<<1 || i0== -(~(1<<3)) || i0 == 11) ?
                putchar(*(arr1+(1>>1))) :
                putchar(*(i1++ +arr1)), (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1) ?
                        putchar(' ') : 1;
        i0++;
    }
}

3) Use if-statement instead of ?: This includes breaking the comma-line into two line.

main()
{
    int arr1[] = {0x69, 0154,107, 'e',0x79,0157, 117,'v',0x6a} , i0, i1;
    i0=i1^i1;
    i1=i0;
    while(i0 < (-(~(1<<3))+3))
    {
        if (i0 == 1<<1 ||i0== -(~(1<<3)) || i0 == 11)
        {
                putchar(*(arr1+(1>>1)));
        }
        else
        {
                putchar(*(i1++ +arr1));
                if (i0 == 1 >> 1 || i0 == 1<<2 || i0 == (1<<3)-1)
                {
                        putchar(' ');
                }
                else
                {
                    1;  // This does nothing so it can be removed
                }
        }
        i0++;
    }
}

4) Recalculate number-constant to better values Examples

0x69 is the same as 'i'

1 << 1 is the same as 2

-(~(1<<3)) is the same as 9

i1 ^ i1 is the same as 0

1>>1 is the same as 0

main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'} , i0, i1;
    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*(arr1));
        }
        else
        {
                putchar(*(i1++ +arr1));

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

5) Some minor final clean up

main()
{
    int arr1[] = { 'i', 'l', 'k', 'e', 'y', 'o', 'u', 'v', 'j'};  // Move i0 and
                                                                  // i1 to nextt line
    int i0, i1;

    i0 = 0;
    i1 = i0;
    while(i0 < 12)
    {
        if (i0 == 2 || i0 == 9 || i0 == 11)
        {
                putchar(*arr1);
        }
        else
        {
                putchar(*(arr1 + i1));  // Splitted into two lines
                ++i1;

                if (i0 == 0 || i0 == 4 || i0 == 7)
                {
                        putchar(' ');
                }
        }
        i0++;
    }
}

Now the code is pretty easy to read.

like image 92
4 revs, 2 users 99% Avatar answered Oct 13 '22 21:10

4 revs, 2 users 99%