문제

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.

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. e.g.

!(!(~x))

i.e. !! is 2 NOT's

What happens to some value if you perform one NOT is:

If x == 0 then !x == 1, otherwise !x == 0

So, if you would perform another NOT, you invert the truth-value again. i.e.

If x == 0 then !!x == 0, otherwise !!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'".

Also, x & 0xFF takes the least significant byte of your variable. More thoroughly explained here:

What does least significant byte mean?

다른 팁

Assuming x is some unsigned int/short/long/... and you want conditions (if, while...):

a) You´ll have to know that just a value/variable as condition (without a==b or something)
is false if it is 0 and true if it is not 0. So, if x is not 0 (true), one ! will switch it to 0 and the other ! to something not-0-like again (not necessarily the old value, only not 0). If x was 0, the ! will finally result in 0 again (first not 0, then again 0).
The whole value of x is not 0 if at least 1 bit is 1...

What you´re doing is to transform either 0 to 0 or a value with 1-bits to some value with 1-bits. Not wrong, but... You can just write if(x) instead of if(!!x)

b) ~ switches every 0-bit to 1 and every 1 to 0. Now you can search again a 1 because you want a 0 in the original value. The same !!-thing again...

c and d:
&0xFF sets all bits except for the lowest 8 ones (lowest byte) to 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. 0xff (decimal 255) is the number which has exactly the lowest 8 bits set to 1...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top