[英]Logical operators and bit manipulation in C
i am trying to do some exercises, but i'm stuck at this point, where i can't understand what's happening and can't find anything related to this particular matter (Found other things about logical operators, but still not enough) 我正在尝试做一些练习,但是我被困在这一点上,在那里我无法理解正在发生的事情,也找不到与该特定问题相关的任何东西(找到了关于逻辑运算符的其他信息,但仍然不够)
EDIT: Why the downvote, i was pretty explicit. 编辑:为什么不赞成投票,我相当明确。 There is no information regarding the type of X, but i assume is INT, the size is not described either, i thought i would discover that by doing the exercise.
没有有关X类型的信息,但我假设是INT,也没有描述大小,我想我会通过练习来发现这一点。
a) At least one bit of x is '1';
b) At least one bit of x is '0';
c) At least one bit at the Least Significant Byte of x , is '1';
d) At least one bit at the Least Significant Byte of x , is '0';
I have the solutions, but would be great to understand them 我有解决方案,但很高兴了解它们
a) !!x // What happens here? The '!' Usually is NOT in c
b) !!~x // Again, the '!' appears... The bitwise operand NOT is '~' and it makes the int one's complement, no further realization made unfortunately
c) !!(x & 0xFF) // I've read that this is a bit mask, i think they take in consideration 4 bytes in X, and this applies a mask at the least significant byte?
d) !!(~x & 0xFF) // Well, at this point i'm lost ...
I would love not having to skip classes at college, but i work full time in order to pay the fees :( . 我不希望不必在大学里上课,但我全职工作以支付费用:(。
You can add brackets around the separate operations and apply them in order. 您可以在各个操作之间添加方括号,然后按顺序应用它们。 eg
例如
!(!(~x))
ie !! 即! is 2 NOT's
是2不是
What happens to some value if you perform one NOT is: 如果执行一个“不”运算,将对某些值产生什么影响:
If x == 0
then !x == 1
, otherwise !x == 0
如果
x == 0
则!x == 1
,否则!x == 0
So, if you would perform another NOT, you invert the truth-value again. 因此,如果您要执行另一个NOT,则将再次反转真值。 ie
即
If x == 0
then !!x == 0
, otherwise !!x == 1
如果
x == 0
则!!x == 0
,否则!!x == 1
You could see it as getting your value between 0 and 1 in which 0 means: "no bit of x is '1'", and 1 means: "at least one bit of x is '1'". 您可能会看到它的值介于0到1之间,其中0表示:“ x的任何一位都不是'1'”,而1表示:“ x的至少一位是'1'”。
Also, x & 0xFF
takes the least significant byte of your variable. 同样,
x & 0xFF
占用变量的最低有效字节。 More thoroughly explained here: 这里更全面地解释:
Assuming x is some unsigned int/short/long
/... and you want conditions (if, while...): 假设x是一些
unsigned int/short/long
/ ...,并且您需要条件(如果,while ...):
a) You´ll have to know that just a value/variable as condition (without a==b
or something) a)您必须知道,只有一个值/变量作为条件(没有
a==b
或其他东西)
is false
if it is 0
and true if it is not 0
. 是
false
,如果它是0
和真实的,如果它不是0
。 So, if x
is not 0
( true
), one !
因此,如果
x
不为0
( true
),则为1 !
will switch it to 0
and the other !
将其切换为
0
,另一个!
to something not-0-like again (not necessarily the old value, only not 0). 再次变为非0的值(不一定是旧值,仅不是0)。 If
x
was 0
, the !
如果
x
为0
,则!
will finally result in 0
again (first not 0
, then again 0
). 最终将再次导致
0
(首先不是0
,然后是0
)。
The whole value of x
is not 0
if at least 1 bit is 1... 如果至少1位为1,则
x
的整个值不为0
...
What you´re doing is to transform either 0 to 0 or a value with 1-bits to some value with 1-bits. 您要做的是将0或0或1位的值转换为1位的值。 Not wrong, but... You can just write
if(x)
instead of if(!!x)
没错,但是...您可以只写
if(x)
而不是if(!!x)
b) ~
switches every 0
-bit to 1
and every 1
to 0
. b)
~
将每个0
位切换为1
,将每个1
切换为0
。 Now you can search again a 1
because you want a 0
in the original value. 现在,您可以再次搜索
1
因为您希望原始值是0
。 The same !!-thing again... 同样的!!-再次...
c and d: c和d:
&0xFF
sets all bits except for the lowest 8 ones (lowest byte) to 0
. &0xFF
将除最低8位(最低字节)以外的所有位都设置为0
。
The result of A&B
is a value where each bit is only 1
if the bits of A an B at the same position are both 1
. A&B
的结果是一个值,如果在相同位置的A&B
位都为1
则每个位仅为1
。 0xff
(decimal 255) is the number which has exactly the lowest 8 bits set to 1
... 0xff
(十进制255)是恰好将最低8位设置为1
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.