469,612 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Bit Operator in C#

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
Dec 12 '05 #1
5 7956
if( (checkLogin & 1) == 1) {
}

if( (checkLogin & 2) == 2 && txtEmpNum.Text.Length > 0) {
}

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:9A**********************************@microsof t.com...
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

Dec 12 '05 #2
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)
{ }

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com


"Dave" wrote:
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

Dec 12 '05 #3
Hi Dave,

"Dave" wrote:
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)
{ }

--
Vadym Stetsyak aka Vadmyst


&& 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,
--
Tom Tempelaere.

Dec 12 '05 #4
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" wrote:
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)
{ }

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com


"Dave" wrote:
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

Dec 12 '05 #5
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

"Dave" <Da**@discussions.microsoft.com> schreef in bericht
news:9A**********************************@microsof t.com...
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

Dec 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by | last post: by
1 post views Thread by joesoap | last post: by
reply views Thread by Martin Magnusson | last post: by
6 posts views Thread by YUY0x7 | last post: by
3 posts views Thread by gugdias | last post: by
8 posts views Thread by valerij | last post: by
3 posts views Thread by y-man | last post: by
18 posts views Thread by Ranganath | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.