Home All Groups Group Topic Archive Search About

problem with logical operator

Author
19 Jan 2006 4:47 AM
ThunderMusic
Hi,
I have a value that contains flags that I must get using a bitmask. I tryied
with the && operator, but the compiler outputs this error :

Operator '&&' cannot be applied to operands of type 'int' and 'int'

The code is the following :

// MASKS.Insert = 2
if (_RightMask && (int)MASKS.Insert)
{
    do something
}

I tried with and without the cast (int), but nothing works...  Am I missing
something? it seems to be the right way of doing it but the compiler does
not allow it.

Thanks

ThunderMusic

Author
19 Jan 2006 6:07 AM
Sergey Poberezovskiy
Use & instead of &&.

Show quote
"ThunderMusic" wrote:

> Hi,
> I have a value that contains flags that I must get using a bitmask. I tryied
> with the && operator, but the compiler outputs this error :
>
> Operator '&&' cannot be applied to operands of type 'int' and 'int'
>
> The code is the following :
>
> // MASKS.Insert = 2
> if (_RightMask && (int)MASKS.Insert)
> {
>     do something
> }
>
> I tried with and without the cast (int), but nothing works...  Am I missing
> something? it seems to be the right way of doing it but the compiler does
> not allow it.
>
> Thanks
>
> ThunderMusic
>
>
>
Author
19 Jan 2006 3:10 PM
David Anton
As Sergey says; use &.

& and | are the C# bitwise operators.  They are also overloaded to be the C#
non-short-circuit logical operators.  (&& and || are the short-circuit
logical operators.)

(In VB, "And" and "Or" are both the bitwise operators and non-short-circuit
logical operators, while "AndAlso" and "OrElse" are the short-circuit logical
operators).

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



Show quote
"ThunderMusic" wrote:

> Hi,
> I have a value that contains flags that I must get using a bitmask. I tryied
> with the && operator, but the compiler outputs this error :
>
> Operator '&&' cannot be applied to operands of type 'int' and 'int'
>
> The code is the following :
>
> // MASKS.Insert = 2
> if (_RightMask && (int)MASKS.Insert)
> {
>     do something
> }
>
> I tried with and without the cast (int), but nothing works...  Am I missing
> something? it seems to be the right way of doing it but the compiler does
> not allow it.
>
> Thanks
>
> ThunderMusic
>
>
>

AddThis Social Bookmark Button