473,659 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8398
if( (checkLogin & 1) == 1) {
}

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

"Dave" <Da**@discussio ns.microsoft.co m> wrote in message
news:9A******** *************** ***********@mic rosoft.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**@discussio ns.microsoft.co m> schreef in bericht
news:9A******** *************** ***********@mic rosoft.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
8026
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 something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
30
10420
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. However, I've come upon a question that I can neither answer from "The Book" or a Google search (so yes, at least I RTFBed). I'm hoping that someone in this news group might know the answer. Overloading the Operator Say I want to develop a...
1
3867
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 "BitString.h"
0
1829
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 be disabled in a release build, and to do that efficiently, I suppose I need to overload the << operator. My current implementation of the stream in release mode is posted below. Everything works fine for POD type like bool and int, but the...
6
4425
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
18816
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 '<' token <snip> --------------------------------------------------- template <class T> class matriz;
5
2290
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
8
2481
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 something to do with the compiler calling the destructor twice. Could someone point out where I go wrong? P.S.: The error it gives is "Debug Assertion Failure ....." (at run time) P.P.S: Everything else works just fine (without the use of...
3
3272
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, obj2 ;
18
3224
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4338
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2757
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.