# Is My Understanding of the & Operator Correct?

449

I am a complete beginner, just trying to make sure I understand each section of the manual before progressing to the next.

My understanding of the & operator is as follows:

& compares the binary digits of two integers and returns a new integer. If the digits from Integer1 correspond with a '1' wherever the digits from Integer2 also have a '1' then the statement can be executed. Otherwise the statement cannot be executed.

For Example:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

YES;int = 1 (00000001)

NO;int = 2  (00000010)

showPopup(button:int)

if(button & YES)

if(button & NO)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In the above example, my understanding is that if the user clicks the button 'YES' then 'button' will return the binary digits of 00000001. If so then the '1' in 'button' would line up with the '1' in 'YES' and the statement could be executed.

Along the same lines, the following statement - 'if(button & NO)' - would not be executed because the binary digits of the button clicked were 00000001, in which the '1' would not line up with the '1' in the 'NO' statement (00000010).

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Is my understanding correct? Or have I got this totally wrong?

2281

You aren't totally wrong, but your example would be better if you set NO = 0. Then you have conventional boolean logic, which is appropriate for a push button.

Thus, you have:

YES = 1 = true = 00000001

NO = 0 = false = 00000000

- - - - - - - - - - - - - - - - - - -

As for bitwise "and," that is using the "&" operator, the following are all true because the last binary digit (bit) is 1.

1 & 1 (00000001 & 00000001) = 1 (00000001)

3 & 1 (00000011 & 00000001) = 1 (00000001)

5 & 1 (00000101 & 00000001) = 1 (00000001)

7 & 1 (00000111 & 00000001) = 1 (00000001)

This Wikipedia article describes bitwise masking in more detail.

449

Anthony Garot:

You aren't totally wrong, but your example would be better if you set NO = 0. Then you have conventional boolean logic, which is appropriate for a push button.

Thus, you have:

YES = 1 = true = 00000001

NO = 0 = false = 00000000

- - - - - - - - - - - - - - - - - - -

As for bitwise "and," that is using the "&" operator, the following are all true because the last binary digit (bit) is 1.

1 & 1 (00000001 & 00000001) = 1 (00000001)

3 & 1 (00000011 & 00000001) = 1 (00000001)

5 & 1 (00000101 & 00000001) = 1 (00000001)

7 & 1 (00000111 & 00000001) = 1 (00000001)

This Wikipedia article describes bitwise masking in more detail.

Hi Anthony,

I was under the impression that bitwise operators were not of boolean type?

I am aware that they are similar but my understanding was that a boolean will simply return 'true' or 'false' whereas the '&' operator will create a new integer with the value of '1' wherever the two compared integers also had a '1'.

Is this not correct?

Moderator
8684

 Operation Desciption Execution Order ()[]. Function CallReferencing to an array elementReferencing to a structure element From left to right !~—++--(type)sizeof Logical negationBitwise negation (complement)Sign changingIncrement by oneDecrement by oneTypecastingDetermining size in bytes Right to left */% MultiplicationDivisionModule division From left to right +— AdditionSubtraction From left to right <<>> Left shiftRight shift From left to right <<=>>= Less thanLess than or equalGreater thanGreater than or equal From left to right ==!= EqualNot equal From left to right & Bitwise AND operation From left to right ^ Bitwise exclusive OR From left to right | Bitwise OR operation From left to right && Logical AND operation From left to right || Logical OR operation From left to right ?: Conditional Operator Right to left =*=/=%=+=-=<<=>>=&=^=|= AssignmentMultiplication with assignmentDivision with assignmentModule with assignmentAddition with assignmentSubtraction with assignmentLeft shift with assignmentRight shift with assignmentBitwise AND with assignmentExclusive OR with assignmentBitwise OR with assignment Right to left , Comma From left to right

### Bitwise AND Operation

The bitwise AND operation of binary-coded x and y representations. The value of the expression contains a 1 (TRUE) in all digits where both x and y contain non-zero, and it contains 0 (FALSE) in all other digits.

 b = ((x & y) != 0);

Example:

 char a='a',b='b'; //--- AND operation    char c=a&b;    Print("a = ",a,"  b = ",b);    Print("a & b = ",c); // The result will be: // a = 97   b = 98 // a & b = 96

449

Marco vd Heijden:
 Operation Desciption Execution Order ()[]. Function CallReferencing to an array elementReferencing to a structure element From left to right !~—++--(type)sizeof Logical negationBitwise negation (complement)Sign changingIncrement by oneDecrement by oneTypecastingDetermining size in bytes Right to left */% MultiplicationDivisionModule division From left to right +— AdditionSubtraction From left to right <<>> Left shiftRight shift From left to right <<=>>= Less thanLess than or equalGreater thanGreater than or equal From left to right ==!= EqualNot equal From left to right & Bitwise AND operation From left to right ^ Bitwise exclusive OR From left to right | Bitwise OR operation From left to right && Logical AND operation From left to right || Logical OR operation From left to right ?: Conditional Operator Right to left =*=/=%=+=-=<<=>>=&=^=|= AssignmentMultiplication with assignmentDivision with assignmentModule with assignmentAddition with assignmentSubtraction with assignmentLeft shift with assignmentRight shift with assignmentBitwise AND with assignmentExclusive OR with assignmentBitwise OR with assignment Right to left , Comma From left to right

### Bitwise AND Operation

The bitwise AND operation of binary-coded x and y representations. The value of the expression contains a 1 (TRUE) in all digits where both x and y contain non-zero, and it contains 0 (FALSE) in all other digits.

 b = ((x & y) != 0);

Example:

 char a='a',b='b'; //--- AND operation    char c=a&b;    Print("a = ",a,"  b = ",b);    Print("a & b = ",c); // The result will be: // a = 97   b = 98 // a & b = 96

Hi Marco,

That is just copy and pasted straight from the manual which I am already reading from mate.

Not really sure what the point in that was lol...

2281

koranged:

I was under the impression that bitwise operators were not of boolean type?

You are mixing your metaphor. Operators are not types and types are not operators.

Bitwise operators act upon types of: boolean, int, long, char, etc. (but not on floats or doubles)

But bitwise operators use "boolean logic." That all sounds confusing, doesn't it?

Think in terms of what's really going on. There are NAND and NOR gates that operate on high/low voltage values.

a boolean will simply return 'true' or 'false'

A boolean variable holds a 'true' or 'false'. These are, in binary, 1 and 0, but since MQL booleans are "a whole number 1 byte large," technically these are 00000001 and 00000000. But you can mix types (so long as not float/double). The following is perfectly acceptable:

```bool b = 1;
int c = 3;
Print (b&c);```

My point, above, wasn't with your understanding of bitwise arithmetic. My point is that you would never use integer values of 1 and 2 to denote YES and NO states of a button. It simply isn't done. That's the whole point of the boolean type.

Now a tri-state button, yes, you might use something like:

OFF = 0 = 00000000
YES = 1 = 00000001
N/A = 2 = 00000011

Then you would check the state doing something like:

```if ( state & 2 == 2 )
{
Print("Is N/A");
}```
449

Anthony Garot:

You are mixing your metaphor. Operators are not types and types are not operators.

Bitwise operators act upon types of: boolean, int, long, char, etc. (but not on floats or doubles)

But bitwise operators use "boolean logic." That all sounds confusing, doesn't it?

Think in terms of what's really going on. There are NAND and NOR gates that operate on high/low voltage values.

A boolean variable holds a 'true' or 'false'. These are, in binary, 1 and 0, but since MQL booleans are "a whole number 1 byte large," technically these are 00000001 and 00000000. But you can mix types (so long as not float/double). The following is perfectly acceptable:

My point, above, wasn't with your understanding of bitwise arithmetic. My point is that you would never use integer values of 1 and 2 to denote YES and NO states of a button. It simply isn't done. That's the whole point of the boolean type.

Now a tri-state button, yes, you might use something like:

OFF = 0 = 00000000
YES = 1 = 00000001
N/A = 2 = 00000011

Then you would check the state doing something like:

Hi Anthony, my apologies I have only just noticed this reply.

Okay I understand the principle of using int value 0 and 1 rather than 1 and 2, that makes sense.

The confusion I have is more over the wording of 'if(button & YES)' or to use your recent example - 'if ( state & 2 == 2 )'

What is 'button'?

In this code 'button' is never assigned with a value so how can the binary digits be compared with another integer if there are no binary digits?

Is it the case that 'button' is in fact representing what button the user pressed? And for example if the button pressed was 'YES' then this will return binary digits '00000001' which would obviously correspond with the binary digits of 'YES', therefore making the condition 'if(button & YES)'  true.

Is this correct or have I just gone off on another tangent haha?

Either way thanks for your help bud.

Moderator
8684

koranged:

Hi Marco,

That is just copy and pasted straight from the manual which I am already reading from mate.

Not really sure what the point in that was lol...

To illustrate the difference between logical and bitwise.