Bit Operator in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am a VB programmer tring to learn C#

I am stuck on a bit operator "&"

in VB:
' checkLogin = 3 means "Login exists" and "Employee Number exists"
If (checkLogin And 1) Then
lbError.Text = "Login already exists"
End If
If (checkLogin And 2) And txtEmpNum.Text <> 0 Then
lbError.Text &= "Employee Number already exists"
End If

I did this in C#:
if(checkLogin & 1)
{
lbError.Text = "Login already exists";
}
if((checkLogin & 2) & txtEmpNum.Text.Length > 0 )
{
lbError.Text = "Employee Number already exists";
}

Error:
Cannot implicitly convert type 'int' to 'bool'

--
Thanks in advance,
Dave



Sorry for the cross post, I selected the wrong group
 
if( (checkLogin & 1) == 1) {
}

if( (checkLogin & 2) == 2 && txtEmpNum.Text.Length > 0) {
}
 
got my answer, Thanks

in C# if you type & it is bit AND operator, it returs int, that is the
reason why the compiler generated error.

In order to get boolean AND comparison you have to write &&

if(checkLogin && 1)
{ }
 
Hi Dave,

Dave said:
got my answer, Thanks

in C# if you type & it is bit AND operator, it returs int, that is the
reason why the compiler generated error.

In order to get boolean AND comparison you have to write &&

if(checkLogin && 1)
{ }

&& is the logical and, and for x being a bool value, the expression
x && true
equals
x

The expression
x && 1
with x being an int is an invalid expression because operator && takes two
bools as its operands.

I've read VB.NET code once, and what they did with the logical operators and
bit-wise operators is just plain ugly and counter-intuative. I've never been
a VB fan though.

Anyway,
operator &: bit-wise AND
operator &&: logical AND

operator |: bit-wise OR
operator ||: logical OR

The bit-wise operators can take any integer value type (short, int, long
....), while the logical operators take bool as operands.

For instance:

15 & 4 yields 4
true && false yields false

11 | 4 yields 15
true || false yields true

Note however that the logical operators && and || are short-circuiting. An
explanation:

For the logical && operator. If in the expression
x && y
statement x yields false, then statement y is not executed since "false &&
y" yields false. For instance in the expression
f( ) && g( )
if method f returns false, method g is not executed and the result of the
expression is false.

For the logical || operator. If in the expression
x || y
statement x yields true, then statement y is not executed since "true || y"
yields true. For instance in the expression
f( ) || g( )
if method f returns true, method g is not executed and the result of the
expression is true.

Kind regards,
 
Actually, & (and |) is both a bitwise operator and non-short-circuiting
operator.
In VB, "And" (and "Or") is both a bitwise operator and non-short-circuiting
operator.

There is no difference between languages in this area apart from the fact
that C# uses &, |, &&, and ||, while VB uses words. In both languages, the
bitwise operators are overloaded to also serve as non-short-circuiting
logical operators.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB.NET to C# Converter
Instant VB: C# to VB.NET Converter
Instant C++: C# to C++ Converter
Instant J#: VB.NET to J# Converter
 
Dave,

In additions to all earlier answers.

The C# && is exactly the same as in VBNet "AndAlso"

VB uses the "And" both for bitwise and for non short-circuiting logical
compare operations.

(Adviced is to use the And not anymore for logical operations. It is as it
was because of backwards compatibility with classic VB)

Cor
 
Back
Top