# bitwise & question

This topic is 4450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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)

##### Share on other sites
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]

##### Share on other sites
The result is 0 for that AND operation, not 4.

If statements that don't contain any comparisions are true is the result is non-zero, false if zero.

if( 4 & ( 1 << 31 ) ) would be false.

Edit: blah, beaten.

##### Share on other sites
if (x) evaluates to true for every non-zero x.

This is a common way of checking if a bit or flag is set. Due to nature of logical and, the result will be 0 if the bit is not set, and non-zero if it is.

##### Share on other sites
Do you know binary?

Example:

1010 & 1001 = 1000

Do a bitwise and for each bit index individually using the following table:

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by n0obDo you know binary?Example:1010 & 1001 = 1000Do a bitwise and for each bit index individually using the following table:0 & 0 = 00 & 1 = 01 & 0 = 01 & 1 = 1

yes i know binary

##### Share on other sites
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).

##### Share on other sites
There are 10 kinds of people in the world: those who know binary and those who don't. :)

[Edited by - blaze02 on April 16, 2006 11:59:48 PM]

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 11
• 21
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631405
• Total Posts
2999890
×