gpt4 book ai didi

c - 这个❤代码是如何工作的?

转载 作者:太空狗 更新时间:2023-10-29 16:40:56 25 4
gpt4 key购买 nike

我在 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;
_++;
}
}

程序的输出是i like you viji。这很感人,但很神秘。所以我用 indent 格式化了它以获得更好的想法。

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;
_++;
}
}

现在不那么感人了,但还是有点神秘。

那么,有人能解释一下这段代码是如何打印出 i like you viji 的吗?

更新:

为变量 $___ 以及扩展的三元运算符提供更好的名称:

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++;
}

最佳答案

重写代码给出:

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) 重新格式化行,删除不必要的空格,插入空格以提高可读性

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) 重命名变量,即 $ 为 arr1,_ 为 i0 和 __ 为 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) 使用 if 语句代替 ?:这包括将逗号行分成两行。

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) 重新计算数值常数以获得更好的值例子

0x69 与 'i' 相同

1 << 1 与 2 相同

-(~(1<<3)) 等同于9

i1 ^ i1 与 0 相同

1>>1等于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) 一些小的最后清理

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++;
}
}

现在代码很容易阅读。

关于c - 这个❤代码是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28419002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com