🔒 Locked
bitwise & question
Advertisement
Original Post
say value = 4 and MASK = 1 <<31; 00000000 00000000 00000000 000000100 & 10000000 00000000 00000000 000000000 --------------------------------------------------------- 00000000 00000000 00000000 000000100 = 4 how does it know when its true or false for this if statement?? if(value&MASK)
Quote:
Original post by 31337noob
00000000 00000000 00000000 000000100
& 10000000 00000000 00000000 000000000
---------------------------------------------------------
00000000 00000000 00000000 000000100 = 4
Thats wrong. anything & 0 is 0. Each bit with 1 is being and'd with a zero, thus, the result is 0.
And to answer your question, its true if the result is anything but 0. Anything.
[Edited by - blaze02 on April 16, 2006 11:50:05 PM]
Quote:
Original post by blaze02 Quote:
Original post by 31337noob
00000000 00000000 00000000 000000100
& 10000000 00000000 00000000 000000000
---------------------------------------------------------
00000000 00000000 00000000 000000100 = 4
Thats wrong. anything & 0 is 0. Each bit with 1 is being and'd with a zero, thus, the result is 0.
And to answer your question, its true if the result is anything but 0. Anything.
thanks
Here's the deal with bitwise operators when comparing bits A and B, and storing the result in bit C (i.e. A&B = C):
OR Operator
-----------
If A or B is set to 1, then C is set to one; otherwise, C is set to zero.
1|1 = 1
1|0 = 1
0|1 = 1
0|0 = 0
Used, for example, when turning on a bit in a bit field.
AND Operator
------------
If A and B are both set to 1, then C is set to one; otherwise, C is set to zero.
1&1 = 1
1&0 = 0
0&1 = 0
0&0 = 0
Used when testing a bit in a bit field, or can be used to turn off a bit in a bit field. If you had a field 11011 and wanted to test bit 2, you could AND it with a sequence containing only bit 2 turned on (00010), which would return the same sequence. If you wanted to turn off bit 2, you could AND the original sequence with the NOT of the second sequence (11101). 11011 & !11101 = 11001.
XOR Operator
------------
If A and B are equal to each other, then C is set to zero; otherwise, C is set to one.
1&1 = 0
1&0 = 1
0&1 = 1
0&0 = 0
In C/C++, you can use the operators like this:
A & B = C; //AND operator
A | B = C; //OR operator
A ^ B = C; //XOR operator
A = !B; //NOT operator, setting A to the inverse of B (if B = 11011, A = 00100).
OR Operator
-----------
If A or B is set to 1, then C is set to one; otherwise, C is set to zero.
1|1 = 1
1|0 = 1
0|1 = 1
0|0 = 0
Used, for example, when turning on a bit in a bit field.
AND Operator
------------
If A and B are both set to 1, then C is set to one; otherwise, C is set to zero.
1&1 = 1
1&0 = 0
0&1 = 0
0&0 = 0
Used when testing a bit in a bit field, or can be used to turn off a bit in a bit field. If you had a field 11011 and wanted to test bit 2, you could AND it with a sequence containing only bit 2 turned on (00010), which would return the same sequence. If you wanted to turn off bit 2, you could AND the original sequence with the NOT of the second sequence (11101). 11011 & !11101 = 11001.
XOR Operator
------------
If A and B are equal to each other, then C is set to zero; otherwise, C is set to one.
1&1 = 0
1&0 = 1
0&1 = 1
0&0 = 0
In C/C++, you can use the operators like this:
A & B = C; //AND operator
A | B = C; //OR operator
A ^ B = C; //XOR operator
A = !B; //NOT operator, setting A to the inverse of B (if B = 11011, A = 00100).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement