bitwise & question

Started by
7 comments, last by blaze02 18 years ago
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)
Advertisement
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]
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
The result is 0 for that AND operation, not 4.

"if(value&MASK)"

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.
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.
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
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
Quote:Original post by n0ob
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



yes i know binary
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).
my siteGenius is 1% inspiration and 99% perspiration
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]
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog

This topic is closed to new replies.

Advertisement