0!= 在此代码中做什么?

wen*_*n32 1 c obfuscation deobfuscation

0 !=这段代码做了什么:

#include <stdio.h>
int main()
{
    int i;
    for(i=0;i<8*5;i++)
        printf("%d",0 != ("HELLO"[i/8] & 1 << (~i&7)) );
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是一个简单的字符串到二进制的转换。我理解,`("HELLO"[i/8] & 1 << (~i&7))但我无法理解该0 !=部分,如果我删除它,它就不起作用。

有没有深入教授按位运算的网站?

Cor*_*bin 6

a != b
Run Code Online (Sandbox Code Playgroud)

是一个等于 true 或 false 的表达式。在 C 中,没有trueand false,而是 0 被认为是 false,而其他任何值都被认为是 true。诸如此类的表达式,a != b如果为假,则返回 0;如果为真,则返回 1。%d 是打印整数的格式字符串,因此 printf() 将打印这个 0 或 1。

在这种情况下,a 是 0,b 是另一边表达式的野兽。("HELLO"[i/8] & 1 << (~i&7)) 详细说明一下其他操作:

("HELLO"[i/8] & 1 << (~i&7))应该添加括号以使其更清楚:

(("HELLO"[i/8]) & (1 << (~i&7)))
Run Code Online (Sandbox Code Playgroud)

(~i&7) 取 i,翻转其中的所有位,然后将所有位与 7 的位进行与。

(1 << (~i&7)))
Run Code Online (Sandbox Code Playgroud)

取 1 并左移 ~i&7 位。

a & b
Run Code Online (Sandbox Code Playgroud)

获取aANDS中的所有位b

几个链接:

http://en.wikipedia.org/wiki/Bitwise_operation

http://www.cprogramming.com/tutorial/bitwise_operators.html

http://msdn.microsoft.com/en-us/library/17zwb64t.aspx