This question already has answers here:
Ordering Output in MPI
(4 answers)
Closed 8 years ago.
I am working on a simple MPI assignment that does row wise Matrix Multiplication.
I am trying to output the matrix and for some reason the order of the prints is out of order.
Only one process is designated to print at a time, the output if flushed, and a MPI_Barrier is used. So I am confused how the prints are being reordered.
void print_matrix(int id, int p, int pn, int n, double **row, double *shared_col_data, double *resdata){
int i,j, k;
for(i=0; i<p; i++){
for(k=0; k < pn; k++){
int row_pos=((i*pn)+k);
if(id==i){
if( row_pos <n){
printf("[ROW: %10d][ID: %3d]\t",row_pos,id);
printf("|");
for(j=0; j<n; j++){
printf("%.4f ",row[k][j]);
}
if(row_pos == (n/2)){
printf("| X |");
}
else{
printf("| |");
}
printf("%.4f ",shared_col_data[row_pos]);
fflush(stdout);
}
}//End its ur turn to print
MPI_Barrier (MPI_COMM_WORLD);
if(id==0){
if(row_pos == (n/2)){
printf("| = |");
}
else{
printf("| |");
}
printf("%.4f|\n",resdata[row_pos]);
fflush(stdout);
}
MPI_Barrier (MPI_COMM_WORLD);
}
}//End Processor Loop
}
Here is a sample output:
Input Size(N): 12
Processors(P): 12
N per Processors(P): 1
ID: 3 SN: 3 EN: 3
ID: 0 SN: 0 EN: 0
ID: 2 SN: 2 EN: 2
ID: 4 SN: 4 EN: 4
ID: 11 SN: 11 EN: 11
ID: 7 SN: 7 EN: 7
ID: 8 SN: 8 EN: 8
ID: 6 SN: 6 EN: 6
ID: 10 SN: 10 EN: 10
ID: 1 SN: 1 EN: 1
ID: 9 SN: 9 EN: 9
ID: 5 SN: 5 EN: 5
All Data Generated
Column Data Shared
Multiplication Done
All res Gather
[ROW: 0][ID: 0] |0.5974 0.7066 0.9131 0.1548 0.4382 0.5132 0.3729 0.5554 0.7832 0.7953 0.5202 0.6986 | |0.8076 | |2.4959|
[ROW: 1][ID: 1] |0.4320 0.9492 0.2266 0.1211 0.3904 0.9614 0.2000 0.7380 0.4471 0.3622 0.9844 0.1921 | |0.0051 | |2.3152|
[ROW: 2][ID: 2] |0.2821 0.6740 0.9673 0.6623 0.6922 0.9760 0.8697 0.0096 0.6827 0.9590 0.2399 0.1100 | |0.2254 | |2.8286|
[ROW: 3][ID: 3] |0.5915 0.1042 0.7262 0.8395 0.9665 0.9716 0.2252 0.7184 0.6054 0.8336 0.5033 0.2620 | |0.3670 | |2.8024|
| |2.1632|
[ROW: 4][ID: 4] |0.0821 0.3956 0.0252 0.9953 0.3822 0.4278 0.8978 0.7726 0.5235 0.2972 0.3229 0.4520 | |0.1409
[ROW: 5][ID: 5] |0.5684 0.0840 0.5961 0.7087 0.1331 0.1426 0.1554 0.3976 0.2051 0.1481 0.9468 0.7025 | |0.5302 | |2.1380|
| = |2.7801|
[ROW: 6][ID: 6] |0.7347 0.9194 0.3374 0.9823 0.1040 0.3878 0.7086 0.3132 0.4359 0.8223 0.2545 0.8752 | X |0.9129
[ROW: 7][ID: 7] |0.0464 0.6857 0.7146 0.6858 0.3210 0.2477 0.5767 0.2342 0.1406 0.5467 0.4063 0.0733 | |0.1262 | |1.8125|
[ROW: 8][ID: 8] |0.6413 0.1076 0.2843 0.3515 0.9252 0.0349 0.0830 0.5063 0.9232 0.9900 0.5849 0.5612 | |0.0204 | |2.1263|
[ROW: 9][ID: 9] |0.5292 0.3410 0.8543 0.5942 0.5822 0.3245 0.1719 0.9346 0.7611 0.3722 0.9653 0.4368 | |0.5167 | |2.4469|
| |2.6545|
[ROW: 10][ID: 10] |0.7641 0.6008 0.9687 0.9276 0.2462 0.2832 0.0131 0.0390 0.4860 0.6569 0.9390 0.7620 | |0.7972 | |2.0755|
[ROW: 11][ID: 11] |0.7291 0.5953 0.6171 0.0396 0.5790 0.5262 0.5881 0.2631 0.9517 0.5118 0.0835 0.2115 | |0.2310
You can see some of the lines begin with the ends of the other lines, but the code that prints those is surround by a barrier.
I feel like it is a simple issue and I am to tired to see it
Thanks for any help.
MPI specification does not cover parallel I/O. This is purely implementation dependant.
Related
This is a ramp least squares estimation problem, better described in math formula elsewhere:
https://scicomp.stackexchange.com/questions/33524/ramp-least-squares-estimation
I used Disciplined Convex-Concave Programming and DCCP package based on CVXPY. The code follows:
import cvxpy as cp
import numpy as np
import dccp
from dccp.problem import is_dccp
# Generate data.
m = 20
n = 15
np.random.seed(1)
X = np.random.randn(m, n)
Y = np.random.randn(m)
# Define and solve the DCCP problem.
def loss_fn(X, Y, beta):
return cp.norm2(cp.matmul(X, beta) - Y)**2
def obj_g(X, Y, beta, sval):
return cp.pos(loss_fn(X, Y, beta) - sval)
beta = cp.Variable(n)
s = 10000000000000
constr = obj_g(X, Y, beta, s)
t = cp.Variable(1)
t.value = [1]
cost = loss_fn(X, Y, beta) - t
problem = cp.Problem(cp.Minimize(cost), [constr >= t])
print("problem is DCP:", problem.is_dcp()) # false
print("problem is DCCP:", is_dccp(problem)) # true
problem.solve(verbose=True, solver=cp.ECOS, method='dccp')
# Print result.
print("\nThe optimal value is", problem.value)
print("The optimal beta is")
print(beta.value)
print("The norm of the residual is ", cp.norm(X*beta - Y, p=2).value)
Because of the large value s, I would hope to get a solution similar to the least squares estimation. But there is no solution as the output shows (with different solver, dimension of the problem, etc):
problem is DCP: False
problem is DCCP: True
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -0.000e+00 +2e+01 9e-02 1e-04 1e+00 9e+00 --- --- 1 1 - | - -
1 -7.422e-04 +2.695e-09 +2e-01 1e-03 1e-06 1e-02 9e-02 0.9890 1e-04 2 1 1 | 0 0
2 -1.638e-05 +5.963e-11 +2e-03 1e-05 2e-08 1e-04 1e-03 0.9890 1e-04 2 1 1 | 0 0
3 -2.711e-07 +9.888e-13 +2e-05 1e-07 2e-10 2e-06 1e-05 0.9890 1e-04 4 1 1 | 0 0
4 -3.991e-09 +1.379e-14 +2e-07 1e-09 2e-12 2e-08 1e-07 0.9890 1e-04 1 0 0 | 0 0
5 -5.507e-11 +1.872e-16 +3e-09 2e-11 2e-14 2e-10 1e-09 0.9890 1e-04 1 0 0 | 0 0
OPTIMAL (within feastol=1.6e-11, reltol=4.8e+01, abstol=2.6e-09).
Runtime: 0.001112 seconds.
ECOS 2.0.7 - (C) embotech GmbH, Zurich Switzerland, 2012-15. Web: www.embotech.com/ECOS
It pcost dcost gap pres dres k/t mu step sigma IR | BT
0 +0.000e+00 -5.811e-01 +1e+01 6e-01 6e-01 1e+00 2e+00 --- --- 1 1 - | - -
1 -7.758e+00 -2.575e+00 +1e+00 2e-01 7e-01 6e+00 3e-01 0.9890 1e-01 1 1 1 | 0 0
2 -3.104e+02 -9.419e+01 +4e-02 2e-01 8e-01 2e+02 8e-03 0.9725 8e-04 2 1 1 | 0 0
3 -2.409e+03 -9.556e+02 +5e-03 2e-01 8e-01 1e+03 1e-03 0.8968 5e-02 3 2 2 | 0 0
4 -1.103e+04 -5.209e+03 +2e-03 2e-01 7e-01 6e+03 4e-04 0.9347 3e-01 2 2 2 | 0 0
5 -1.268e+04 -1.592e+03 +8e-04 1e-01 1e+00 1e+04 2e-04 0.7916 4e-01 3 2 2 | 0 0
6 -1.236e+05 -2.099e+04 +9e-05 1e-01 1e+00 1e+05 2e-05 0.8979 9e-03 1 1 1 | 0 0
7 -4.261e+05 -1.850e+05 +4e-05 2e-01 7e-01 2e+05 1e-05 0.7182 3e-01 2 1 1 | 0 0
8 -2.492e+07 -1.078e+07 +7e-07 1e-01 7e-01 1e+07 2e-07 0.9838 1e-04 3 2 2 | 0 0
9 -2.226e+08 -9.836e+07 +5e-08 9e-02 5e-01 1e+08 1e-08 0.9339 2e-03 2 3 2 | 0 0
UNBOUNDED (within feastol=1.0e-09).
Runtime: 0.001949 seconds.
The optimal value is None
The optimal beta is
None
The norm of the residual is None
I am learning little by little SIMD programming, and I've devised a (seemingly) simple problem that I hope I can speed-up using SIMD (AVX, at the moment I have access only to AVX CPUs).
I have a long string constituted by an alphabet of 2^k characters (for instance 0, 1, 2, 3), and I'd like to:
generate all substrings of a given length substringlength
convert all the substrings in bits
The substrings are just sequences of characters from the input string:
012301230123012301230123012301233012301301230123123213012301230
substringlength = 6;
string bits
------+--+-----------------
012301 -> 01 00 11 10 01 00
123012 -> 10 01 00 11 10 01
230123 -> 11 10 01 00 11 10
301230 -> 00 11 10 01 00 11
...
My question is due to my inexperience with SIMD (I've only read "Modern x86 Assembly Language Programming", by Kusswurm):
Is this a task where SIMD could help?
Edit: for simplicity, let's just assume k = 2, and so the ASCII numbers will be just '0'..'3'.
Iteration 1
Reading the comments and playing around I've come to these realizations. I can convert the the ASCII into values, and as suggested, multiply-add adjacent bytes:
// SIMD 128-bit registers, apparently I cannot use AVX ones directly (some operations are AVX2 or AVX-512)
__m128i sse, val, adj, res;
auto mask = _mm_set_epi8(1, 1<<4, 1, 1<<4, 1, 1<<4, 1, 1<<4, 1, 1<<4, 1, 1<<4, 1, 1<<4, 1, 1<<4);
auto zero = _mm_set_epi8('0', '0', '0', '0', '0', '0', '0', '0',
'0', '0', '0', '0', '0', '0', '0', '0');
// Load ascii values
sse = _mm_loadu_si128((__m128i*) s.data());
// Convert to integer values
val = _mm_sub_epi8(sse[0], zero);
// Multiply with mask byte by byte (aka SHL second bytes of val) and sum
adj = _mm_maddubs_epi16(val, mask);
An idea of what it does, to people learning like me, is given here (I will need more 128-bits to encode one substring, ascii is in hex):
bytes 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ascii 30 31 30 31 30 31 30 31 30 31 30 31 30 31 30 31
_mm_sub_epi8:
value 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
_mm_maddubs_epi16:
value 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
* * * * * * * * * * * * * * * *
mask 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4
+ + + + + + + +
| | | | | | | | |
(16-bits)
bits ....0100 ....0100 ....0100 ....0100 ....0100 ....0100 ....0100 ....0100
In other words the first 4 bits are correct, encoding 2 ascii chars, if I understand correctly what _mm_maddubs_epi16 did to my values, which I am not sure at all!
Now I'd need some sort of "shift-or" of adjacent bytes, something like _mm_maddubs_epi16 that shifts left the first, and ORs with the second argument, producing an 8-bit or 16-bit value:
(16-bits)
bits ....0100 ....0100 ....0100 ....0100 ....0100 ....0100 ....0100 ....0100
| shl 4 | | shl 4 | | shl 4 | | shl 4 |
0100.... ....0100 0100.... ....0100 0100.... ....0100 0100.... ....0100
OR OR OR OR
....01000100 ....01000100 ....01000100 ....01000100
However, I cannot see how _mm_bslli_si128 could help me here, or if there is a smarter way to do this. Maybe even this "horizontal" approach is foolish, and I have to rethink it.
Any hint is welcome!
Say I have the following 3 columns in text file:
1 003 3
2 006 1
3 005 4
4 001 2
5 006 7
6 002 2
7 004 3
8 001 6
9 002 8
10 005 2
I want to output 3 columns:
last sequence number in every group of 3,
$2 of the max $3 value in every group,
and the max $3 in every group.
Starting from after the first one. So from that input, the output would be:
1 003 3
4 005 4
7 006 7
10 002 8
What I tried:
awk \
'BEGIN{
cnt=3;
max=0;
};
{
if (cnt == 3){
cnt++;
max_arr[cnt]=$3;
for (i in max_arr){
if (max_arr[i] > max)
{ max = max_arr[i] }
}
printf "%s %s %s\n", $1,$2,max;
cnt=1;
delete max_arr;
max=0;
}
else{
cnt++;
max_arr[cnt]=$3;
}
}' input_file.txt
This gives me:
1 003 3
4 001 4
7 004 7
10 005 8
Column 1 and 3 are correct, but 2 is wrong.
This is how you do it robustly:
$ cat tst.awk
{
isBlockBeg = ( (NR%3)==2 )
isBlockEnd = ( (NR%3)==1 )
}
isBlockBeg { max=$3 }
$3 >= max { max=$3; val=$2 }
isBlockEnd { print $1, val, max }
END { if (!isBlockEnd) print $1, val, max }
$ awk -f tst.awk file
1 003 3
4 005 4
7 006 7
10 002 8
Note that the above will work whether your data is numbers or strings, whether or not your data is all-negative, and even if your data doesn't end nicely at the end of a block of 3. If you don't need that last part, you can reduce it to just:
$ cat tst.awk
(NR%3)==2 { max=$3 }
$3 >= max { max=$3; val=$2 }
(NR%3)==1 { print $1, val, max }
$ awk -f tst.awk file
1 003 3
4 005 4
7 006 7
10 002 8
A shorter awk script could be this one:
awk 'm<$3{m=$3;n=$2} !((NR+2)%3){print $1,n,m;m=n=""}' file
where the max value of column 3 is m, the corresponding value of column 2 is n.
The statement !((NR+2)%3) is executed for the first line and every next 3 lines, which print the wanted value and unset both the max value of column 3 m and n.
You could try the following awk script :
# file : script.awk
# if max[1] is uninitialized OR ...
# if the 3rd field of our current line is > than the one stored in our max array ...
# we store the 2nd and 3rd field of our line in the array
!(1 in max) || max[1]<$3 { max[0]=$2; max[1]=$3; }
# if the remainder of our line_number / 3 == 1 (lines 4, 7, 10, ...)
NR % 3 == 1 {
# we print the line_number, and the 2 max values
print NR,max[0],max[1]
# we delete the old array
delete max
}
You can then call it like this : awk -f script.awk data
Sample input :
> cat data
1 003 3
2 006 1
3 005 4
4 001 2
5 006 7
6 002 2
7 004 3
8 001 6
9 002 8
10 005 2
Sample output :
> awk -f script.awk data
1 003 3
4 005 4
7 006 7
10 002 8
if $3 values are all positive...
$ awk '$3>m3 {m3=$3; v2=$2}
NR%3==1 {print $1,v2,m3; m3=0}' file
1 003 3
4 005 4
7 006 7
10 002 8
This question already has answers here:
Multiple value enum in Obj-C
(2 answers)
Closed 9 years ago.
I was looking into how one uses the UISwipeGestureRecognizer class in Objective-C and happened to run across an enum named UISwipeGestureRecognizerDirection.
Apple defines this as:
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
}
I wasn't sure how the compiler interpreted the above, specifically the << operator. From looking it up, it appears to be the bitwise shift left - But I am afraid I dont understand how the operator works above.
Any direction is greatly appreciated. Thanks in advance!
In a computer, integer numbers are represented as 1's and 0's.
So for a 1 bit computer, we can only use 1 or 0:
0 or 1 - 1 is the maximum number.
0 = 0
1 = 1
2-bit computer: - 3 is the maximum number. Remember you aren't
counting from 0 to 9. You can only count to 1 then you have to
increase the next column to the left.
00 = 0 + 0 = 0
01 = 0 + 1 = 1
10 = 2 + 0 = 2
11 = 2 + 1 = 3
3-bit computer: - 7 is the maximum number
000 = 0 + 0 + 0 = 0
001 = 0 + 0 + 1 = 1
010 = 0 + 2 + 0 = 2
011 = 0 + 2 + 1 = 3
100 = 4 + 0 + 0 = 4
101 = 4 + 0 + 1 = 5
110 = 4 + 2 + 0 = 6
111 = 4 + 2 + 1 = 7
All left bit-shifting (1<<3) means is place a 1 at the right most
column (the zero position) and move the specified number of places to the left.
001 - 1<<0 -- Move it 0 times - Value is 1
010 - 1<<0 -- Move it 1 times - Value is 2
100 - 1<<0 -- Move it 2 times - Value is 4
Here's a table to help you think about an 8-bit computer:
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| 128| 64| 32| 16| 8| 4| 2| 1|
| 2^7| 2^6 | 2^5| 2^4| 2^3| 2^2| 2^1| 2^0|
|1 << 7 | 1 << 6 | 1 << 5 | 1 << 4 | 1 << 3 | 1 << 2 | 1 << 1 | 1 << 0 |
It is a bitwise shift left. This is the equivalent of
typedef enum {
UISwipeGestureRecognizerDirectionRight = 1,
UISwipeGestureRecognizerDirectionLeft = 2,
UISwipeGestureRecognizerDirectionUp = 4,
UISwipeGestureRecognizerDirectionDown = 8
}
It's a convenience to allow bitwise testing. The actual value means nothing much except that they all can be packed into one variable - enum values can be anything at all as long as they are unique.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am not a specialist in C/C++.
I found this declaration today:
typedef NS_OPTIONS(NSUInteger, PKRevealControllerType)
{
PKRevealControllerTypeNone = 0,
PKRevealControllerTypeLeft = 1 << 0,
PKRevealControllerTypeRight = 1 << 1,
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft | PKRevealControllerTypeRight)
};
Can you guys translate what values every value will have?
opertor << is bitwise left shift operator. Shift all the bits to left a specified number of times: (arithmetic left shift and reserves sign bit)
m << n
Shift all the bits of m to left a n number of times. (notice one shift == multiply by two).
1 << 0 means no shift so its equals to 1 only.
1 << 1 means one shift so its equals to 1*2 = 2 only.
I explain with one byte: one in one byte is like:
MSB
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 / 0
| / 1 << 1
| |
▼ ▼
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
Whereas 1 << 0 do nothing but its like figure one. (notice 7th bit is copied to preserve sign)
OR operator: do bit wise or
MSB PKRevealControllerTypeLeft
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | == 1
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| | | | | | | | OR
MSB PKRevealControllerTypeRight
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | == 2
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
=
MSB PKRevealControllerTypeBoth
+----+----+----+---+---+---+---+---+
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | == 3
+----+----+----+---+---+---+---+---+
7 6 5 4 3 2 1 0
| is bit wise operator. in below code it or 1 | 2 == 3
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1 << 0, // one
PKRevealControllerTypeRight = 1 << 1, // two
PKRevealControllerTypeBoth = (PKRevealControllerTypeLeft |
PKRevealControllerTypeRight) // three
There is not more technical reason to initialized values like this, defining like that makes things line up nicely read this answer:define SOMETHING (1 << 0)
compiler optimization convert them in simpler for like: (I am not sure for third one, but i think compiler will optimize that too)
PKRevealControllerTypeNone = 0, // is Zero
PKRevealControllerTypeLeft = 1, // one
PKRevealControllerTypeRight = 2, // two
PKRevealControllerTypeBoth = 3, // Three
Edit: #thanks to Till.
read this answer App States with BOOL flags show the usefulness of declarations you got using bit wise operators.
It's an enum of bit flags:
PKRevealControllerTypeNone = 0 // no flags set
PKRevealControllerTypeLeft = 1 << 0, // bit 0 set
PKRevealControllerTypeRight = 1 << 1, // bit 1 set
And then
PKRevealControllerTypeBoth =
(PKRevealControllerTypeLeft | PKRevealControllerTypeRight)
is just the result of bitwise OR-ing the other two flags. So, bit 0 and bit 1 set.
The << operator is the left shift operator. And the | operator is bitwise OR.
In summary the resulting values are:
PKRevealControllerTypeNone = 0
PKRevealControllerTypeLeft = 1
PKRevealControllerTypeRight = 2
PKRevealControllerTypeBoth = 3
But it makes a lot more sense to think about it in terms of flags of bits. Or as a set where the universal set is: { PKRevealControllerTypeLeft, PKRevealControllerTypeRight }
To learn more you need to read up about enums, shift operators and bitwise operators.
This looks like Objective C and not C++, but regardless:
1 << 0
is just one bitshifted left (up) by 0 positions. Any integer "<<0" is just itself.
So
1 << 0 = 1
Similarly
1 << 1
is just one bitshifted left by 1 position. Which you could visualize a number of ways but the easiest is to multiply by 2.[Note 1]
So
x << 1 == x*2
or
1 << 1 == 2
Lastly the single pipe operator is a bitwise or.
So
1 | 2 = 3
tl;dr:
PKRevealControllerTypeNone = 0
PKRevealControllerTypeLeft = 1
PKRevealControllerTypeRight = 2
PKRevealControllerTypeBoth = 3
[1] There are some limitations on this generalization, for example when x is equal to or greater than 1/2 the largest value capable of being stored by the datatype.
This all comes down to bitwise arithmetic.
PKRevealControllerTypeNone has a value of 0 (binary 0000)
PKRevealControllerTypeLeft has a value of 1 (binary 0001)
PKRevealControllerTypeRight has a value of 2 (binary 0010) since 0001 shifted left 1 bit is 0010
PKRevealControllerTypeBoth has a value of 3 (binary 0011) since 0010 | 0001 (or works like addition) = 0011
In context, this is most-likely used to determine a value. The property is & (or bitwise-and) works similar to multiplication. If 1 ands with a number, then the number is preserved, if 0 ands with a number, then the number is cleared.
Thus, if you want to check if a particular controller is specifically type Left and it has a value of 0010 (i.e. type Right) 0010 & 0001 = 0 which is false as we expect (thus, you have determined it is not of correct type). However, if the controller is Both 0011 & 0001 = 1 so the result is true which is correct since we determined this is of Both types.