Reputation: 6615
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++;
}
Upvotes: 20
Views: 846
Reputation: 44329
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++;
}
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.
Upvotes: 23