472,974 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,974 software developers and data experts.

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 8209
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.