bitwise logical operations

  • Thread starter Thread starter Filip Kratochvil
  • Start date Start date
F

Filip Kratochvil

Hello all,

In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then
...
do something here
...
End If

where intValue is for examle 4160 (to satisfy the condition)

if i have a C# consolle application with:
Console.WriteLine(4160 & 64)
I will get 64 as a result - as expected, but how do I write the "If "test
with an integer variable passed in??
writting the following:
if (4160 & 64 == 64)
{ ...
do something here
...
}

I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'

Thanks in advance
Filip
 
Filip Kratochvil said:
In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then : :
if (4160 & 64 == 64) : :
I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'

Just like in VB.NET, Flip, the associativity of the equality operator
supercedes the logical-AND (in this context, it assumes there are
two terms, 4160 (ie, true) and 64 == 64 (ie, true)).

What is needed is a pair of parenthesis (which will force the & to
get interpreted as a bitwise-AND, rather than a logical-AND.

int someInt = 4160;
if ( ( someInt & 64 ) == 64 ) {
// . . . do something . . .
}



Derek Harmon
 
Derek
Thanks for the quick reply
Filip

Derek Harmon said:
Just like in VB.NET, Flip, the associativity of the equality operator
supercedes the logical-AND (in this context, it assumes there are
two terms, 4160 (ie, true) and 64 == 64 (ie, true)).

What is needed is a pair of parenthesis (which will force the & to
get interpreted as a bitwise-AND, rather than a logical-AND.

int someInt = 4160;
if ( ( someInt & 64 ) == 64 ) {
// . . . do something . . .
}



Derek Harmon
 
What your running into is operator precedence...


Try this and it should work...
if ((int)(number & 64) == 64)
 
Back
Top