473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Syntax question and String Comparison

I would like to compare a string value to a pre-determined list of
other strings. Is there a simple way to do this in one statements like
this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what this
statement really is saying) but I'd like to be able to do *something*
like this. One suggestion was to add the compare strings to an
arraylist and do a binary search.

It's sad that my knowledge of and/or operators is so lacking. If anyone
knows of a good link to explain the difference between &&, &, ||, | i
would be greatly appreciative. I don't know how I have survived
without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Any info would be helpful. I don't mind doing the heavy lifting, I just
need a bit of direction.

Cheers,

Russ

Jun 9 '06 #1
17 2010
| is a bitwise operator OR
http://en.wikipedia.org/wiki/Bitwise_operations#OR this operation is
intended for numeric values (not for strings)

|| is a logical OR http://en.wikipedia.org/wiki/Logical_or

Whoever told you to use the arraylist gave you a very good plan for this
case (especially with large numbers of strings).

The binary search will be O(log n) where as using your ors directly (or
looping through a list) will be O(n) ..

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung

"Dinsdale" <ru********@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
I would like to compare a string value to a pre-determined list of
other strings. Is there a simple way to do this in one statements like
this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what this
statement really is saying) but I'd like to be able to do *something*
like this. One suggestion was to add the compare strings to an
arraylist and do a binary search.

It's sad that my knowledge of and/or operators is so lacking. If anyone
knows of a good link to explain the difference between &&, &, ||, | i
would be greatly appreciative. I don't know how I have survived
without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Any info would be helpful. I don't mind doing the heavy lifting, I just
need a bit of direction.

Cheers,

Russ

Jun 9 '06 #2
The difference here is the difference between logical operators and bitwise
operators.

&& and || are logical operators.
& and | are bitwise operators.

A logical operator is used to evaluate an expression, and returns true or
false. The AND operator indicates that the operands on both sides MUST BOTH
evaluate to true. The OR operator indicates that only ONE of the operands
must be true (one OR the other)

X == 1 && X ==2 - False always, since X cannot logically be both 1 AND 2.
Y == 1 && Y < 2 - True, if Y is equal to 1, because 1 is logically less than
2. False if Y is equal to 0, because although 0 is less than 2, 0 is not
logically equal to 1, and thus BOTH can not be true, unless Y is equal to 1.

X < 2 || X > 2 - True, unless X is equal to 2, since 2 is neither less than
2 nor is it greater than 2
X == Y || X != Y True always. If X equals Y, the first part is true. If X is
not equal to Y, the second part is true. Therefore, one OR the other will
always be true.

A bitwise operator is actually a mathematical operator that combines 2
binary values to create a third. the way that this is done is that each bit
in a value is compared with the bit at the same position in another value,
and depending on the operator used, yeilds a 1 or a 0. The resulting value
is the value of the resulting binary number.

Bitwise operators are a "bit" more difficult to understand. Here's a good
reference, with examples:

http://en.wikipedia.org/wiki/Bitwise_operation

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
"Dinsdale" <ru********@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
I would like to compare a string value to a pre-determined list of
other strings. Is there a simple way to do this in one statements like
this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what this
statement really is saying) but I'd like to be able to do *something*
like this. One suggestion was to add the compare strings to an
arraylist and do a binary search.

It's sad that my knowledge of and/or operators is so lacking. If anyone
knows of a good link to explain the difference between &&, &, ||, | i
would be greatly appreciative. I don't know how I have survived
without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Any info would be helpful. I don't mind doing the heavy lifting, I just
need a bit of direction.

Cheers,

Russ

Jun 9 '06 #3
Actually, | and & are also logical operators. They are overloaded to serve
as both bitwise operators and non-short-circuit logical operators (identical
behavior to the VB And/Or operators).
For a more complete discussion, see
http://www.tangiblesoftwaresolutions...d%20CSharp.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
"Kevin Spencer" wrote:
The difference here is the difference between logical operators and bitwise
operators.

&& and || are logical operators.
& and | are bitwise operators.

A logical operator is used to evaluate an expression, and returns true or
false. The AND operator indicates that the operands on both sides MUST BOTH
evaluate to true. The OR operator indicates that only ONE of the operands
must be true (one OR the other)

X == 1 && X ==2 - False always, since X cannot logically be both 1 AND 2.
Y == 1 && Y < 2 - True, if Y is equal to 1, because 1 is logically less than
2. False if Y is equal to 0, because although 0 is less than 2, 0 is not
logically equal to 1, and thus BOTH can not be true, unless Y is equal to 1.

X < 2 || X > 2 - True, unless X is equal to 2, since 2 is neither less than
2 nor is it greater than 2
X == Y || X != Y True always. If X equals Y, the first part is true. If X is
not equal to Y, the second part is true. Therefore, one OR the other will
always be true.

A bitwise operator is actually a mathematical operator that combines 2
binary values to create a third. the way that this is done is that each bit
in a value is compared with the bit at the same position in another value,
and depending on the operator used, yeilds a 1 or a 0. The resulting value
is the value of the resulting binary number.

Bitwise operators are a "bit" more difficult to understand. Here's a good
reference, with examples:

http://en.wikipedia.org/wiki/Bitwise_operation

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
"Dinsdale" <ru********@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
I would like to compare a string value to a pre-determined list of
other strings. Is there a simple way to do this in one statements like
this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what this
statement really is saying) but I'd like to be able to do *something*
like this. One suggestion was to add the compare strings to an
arraylist and do a binary search.

It's sad that my knowledge of and/or operators is so lacking. If anyone
knows of a good link to explain the difference between &&, &, ||, | i
would be greatly appreciative. I don't know how I have survived
without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}

Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Any info would be helpful. I don't mind doing the heavy lifting, I just
need a bit of direction.

Cheers,

Russ


Jun 9 '06 #4
"Dinsdale" <ru********@gmail.com> wrote in
news:11**********************@j55g2000cwa.googlegr oups.com:
I would like to compare a string value to a pre-determined list
of other strings. Is there a simple way to do this in one
statements like this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what
this statement really is saying) but I'd like to be able to do
*something* like this. One suggestion was to add the compare
strings to an arraylist and do a binary search.
Russ,

That's a good suggestion. Another way to search a small number
of strings is to use the System.String.IndexOf method:

if ("string_1|string_2|string_3".IndexOf(strMystring. ToLower()) >= 0)
// string found.
It's sad that my knowledge of and/or operators is so lacking. If
anyone knows of a good link to explain the difference between
&&, &, ||, | i would be greatly appreciative. I don't know how I
have survived without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}
The && operator is for logical "and" operations, as in:

if ((a == 1) && (b == 2))...

The || operator is the logical "or".

The & operator is used for bitwise "and" operations. It's almost
always used to test a number to see if one or more bits in that number
are set to 1.

For example:

// Two eight-bit numbers.
int a = 191; // Binary 10111111
int b = 128; // Binary 10000000

// Does "a" have its left-most bit turned on?
if ((a & b) == b)
// Yes it does.
else
// No it doesn't.
Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD |
Win32Com.EV_RING |
Win32Com.EV_ERR))


The | operator is the bitwise "or" operator. It's usually used
to turn one or more bits on in a number.

For example:

int a = 1; // Binary 0001
int b = 2; // Binary 0010
int c = 4; // Binary 0100
int d = 8; // Binary 1000

// Create a number with the fourth and second bits turned on.
// (NB: The most significant bits are on the left, and the least
// significant bits are on the right. That's why the right-most bit
// is referred to as the first bit).
int e = b | d; // Binary 1010. The bitwise combination of 1000 and 0010.

// Use the & operator to see if a bit is set in e:
if ((e & b) == b) then
// Yes, it's set.

The & and | operators also work with multiple bits, not just the single
bits I've shown in the example code above.

It should be noted that bitwise "or-ing" numbers together is not
a form of addition. 8 | 8 == 8, but 8 + 8 == 16.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Jun 10 '06 #5
"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
"Dinsdale" <ru********@gmail.com> wrote in
news:11**********************@j55g2000cwa.googlegr oups.com:

if ("string_1|string_2|string_3".IndexOf(strMystring. ToLower()) >= 0)
// string found.


Or this, in order to avoid matching substrings:

if ("string_1|string_2|string_3|".IndexOf(strMystring .ToLower() + "|") >=
0)
// string found.
Jun 10 '06 #6
See my other reply: & and | are identical in behavior to VB's 'And' & 'Or' -
they are overloaded to also act as non-short-circuiting logical operators
(operating on bool values).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
"Chris R. Timmons" wrote:
"Dinsdale" <ru********@gmail.com> wrote in
news:11**********************@j55g2000cwa.googlegr oups.com:
I would like to compare a string value to a pre-determined list
of other strings. Is there a simple way to do this in one
statements like this:

if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
{}

This syntax doesn't work (and in fact I'm not quite sure what
this statement really is saying) but I'd like to be able to do
*something* like this. One suggestion was to add the compare
strings to an arraylist and do a binary search.


Russ,

That's a good suggestion. Another way to search a small number
of strings is to use the System.String.IndexOf method:

if ("string_1|string_2|string_3".IndexOf(strMystring. ToLower()) >= 0)
// string found.
It's sad that my knowledge of and/or operators is so lacking. If
anyone knows of a good link to explain the difference between
&&, &, ||, | i would be greatly appreciative. I don't know how I
have survived without knowing these things but here I am. :-}

Some examples of syntax I don't really understand:
Example 1:

if ((eventMask & Win32Com.EV_CTS) != 0)
{
i |= Win32Com.MS_CTS_ON;
}


The && operator is for logical "and" operations, as in:

if ((a == 1) && (b == 2))...

The || operator is the logical "or".

The & operator is used for bitwise "and" operations. It's almost
always used to test a number to see if one or more bits in that number
are set to 1.

For example:

// Two eight-bit numbers.
int a = 191; // Binary 10111111
int b = 128; // Binary 10000000

// Does "a" have its left-most bit turned on?
if ((a & b) == b)
// Yes it does.
else
// No it doesn't.
Example 2:
if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD |
Win32Com.EV_RING |
Win32Com.EV_ERR))


The | operator is the bitwise "or" operator. It's usually used
to turn one or more bits on in a number.

For example:

int a = 1; // Binary 0001
int b = 2; // Binary 0010
int c = 4; // Binary 0100
int d = 8; // Binary 1000

// Create a number with the fourth and second bits turned on.
// (NB: The most significant bits are on the left, and the least
// significant bits are on the right. That's why the right-most bit
// is referred to as the first bit).
int e = b | d; // Binary 1010. The bitwise combination of 1000 and 0010.

// Use the & operator to see if a bit is set in e:
if ((e & b) == b) then
// Yes, it's set.

The & and | operators also work with multiple bits, not just the single
bits I've shown in the example code above.

It should be noted that bitwise "or-ing" numbers together is not
a form of addition. 8 | 8 == 8, but 8 + 8 == 16.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Jun 10 '06 #7
Wow. Thanks for the great response everybody. It's bitwise operations
that I need to work on. Thank you to Chris for that explaination. I'll
surmize that the following statement is sending the bitwise result of
all the operands?

if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))

And I'm also going to guess that you can't use a bitwise operator on
strings because strings are unicode?

I decided to try the binary search idea, which turned out something
like this:

stirng strValue = "String3";
ArrayList alList = new ArrayList(new string[]
{"STRING1","STRING2","STRING3"});
alList.Sort();
if(alList.BinarySearch(strValue) > -1)
{
....
}

Cheers!
Russ

Jun 10 '06 #8
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:OX**************@TK2MSFTNGP04.phx.gbl...
Or this, in order to avoid matching substrings:

if ("string_1|string_2|string_3|".IndexOf(strMystring .ToLower() + "|") >=
0)
// string found.


I should have said

if ("|string_1|string_2|string_3|".IndexOf("|" + strMystring.ToLower() +
"|") >= 0)
// string found.
Jun 10 '06 #9
Greg Young <dr*******************@hotmail.com> wrote:
| is a bitwise operator OR
http://en.wikipedia.org/wiki/Bitwise_operations#OR this operation is
intended for numeric values (not for strings)

|| is a logical OR http://en.wikipedia.org/wiki/Logical_or

Whoever told you to use the arraylist gave you a very good plan for this
case (especially with large numbers of strings).


That's not really a particularly good plan. A far better plan would be
to use a dictionary, where the hashcode is used for coarse searching,
giving a list of strings involved with the same hashcode. That list
will usually be very small.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 10 '06 #10
Mark Wilden <Ma********@newsgroups.nospam> wrote:
"Mark Wilden" <Ma********@newsgroups.nospam> wrote in message
news:OX**************@TK2MSFTNGP04.phx.gbl...
Or this, in order to avoid matching substrings:

if ("string_1|string_2|string_3|".IndexOf(strMystring .ToLower() + "|") >=
0)
// string found.


I should have said

if ("|string_1|string_2|string_3|".IndexOf("|" + strMystring.ToLower() +
"|") >= 0)
// string found.


Unless, of course, strMystring is "string_1|string_2" in which case it
fails.

It's much simpler just to do three comparisons than to try to get
clever with this kind of thing, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 10 '06 #11
Dinsdale wrote:
I'll
surmize that the following statement is sending the bitwise result of
all the operands?

if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
| Win32Com.EV_BREAK |
Win32Com.EV_RLSD | Win32Com.EV_RING |
Win32Com.EV_ERR))
Right.
And I'm also going to guess that you can't use a bitwise operator on
strings because strings are unicode?


Right again.

Well, actually it's because there is no bitwise operator defined for
strings, as it wouldn't make any sense to perform bitwise operations on
strings.
Jun 10 '06 #12
Actually Jon it is a trade off. The hash index would use roughly 2.3 times
the memory as the binary search.

The binary search method can also do other functionalities such as "closest
match".

So it really depends which is important to you.

Cheers,

Greg Young

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Greg Young <dr*******************@hotmail.com> wrote:
| is a bitwise operator OR
http://en.wikipedia.org/wiki/Bitwise_operations#OR this operation is
intended for numeric values (not for strings)

|| is a logical OR http://en.wikipedia.org/wiki/Logical_or

Whoever told you to use the arraylist gave you a very good plan for this
case (especially with large numbers of strings).


That's not really a particularly good plan. A far better plan would be
to use a dictionary, where the hashcode is used for coarse searching,
giving a list of strings involved with the same hashcode. That list
will usually be very small.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 10 '06 #13
Greg Young <dr*******************@hotmail.com> wrote:
Actually Jon it is a trade off. The hash index would use roughly 2.3 times
the memory as the binary search.
Of the index, yes. I suspect that the bulk of the memory would usually
be taken by the strings themselves, unless you're talking about
thousands of very short strings.
The binary search method can also do other functionalities such as "closest
match".
So long as you view "AXXXXXXXXXX" as closer to "AHHHHHHHHHHHHHHHH" than
"ZXXXXXXXXXXXXXXX" :)
So it really depends which is important to you.


What is normally most important to me is code simplicity. Using a
dictionary gives the required functionality for free. No need to sort
things or use a binary search - just ask the dictionary whether or not
it contains the value, using the Contains method. Very simple.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 10 '06 #14
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Unless, of course, strMystring is "string_1|string_2" in which case it
fails.
Of course. I took it as read that you'd choose a separator that couldn't
occur in a string.
It's much simpler just to do three comparisons than to try to get
clever with this kind of thing, IMO.


Actually, the technique I mentioned isn't particularly "clever"; it's been a
standard technique for at least the 20 years I've been in the biz.

I'm not suggesting it's the best way for all applications, but it is "a
way," that's worked for a long time. I was just mentioning it as I was
surprised to see all this talk about logical and bitwise operators, when it
didn't seem anyone understood what was really being suggested to the OP. :)
Jun 10 '06 #15
Yes, I wanted to know about all the different techniques possible.

Thanks again.

Russ

Mark Wilden wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Unless, of course, strMystring is "string_1|string_2" in which case it
fails.


Of course. I took it as read that you'd choose a separator that couldn't
occur in a string.
It's much simpler just to do three comparisons than to try to get
clever with this kind of thing, IMO.


Actually, the technique I mentioned isn't particularly "clever"; it's been a
standard technique for at least the 20 years I've been in the biz.

I'm not suggesting it's the best way for all applications, but it is "a
way," that's worked for a long time. I was just mentioning it as I was
surprised to see all this talk about logical and bitwise operators, when it
didn't seem anyone understood what was really being suggested to the OP. :)


Jun 11 '06 #16
> Actually, | and & are also logical operators. They are overloaded to
serve
That's just a matter of which semantics you're using, which is generally
dependent upon what you're doing with them. They are actually, like anything
else in a computer processor, mathematical operators. I found the semantics
I used to be helpful is expressing the difference between their general
usage.

You say "tomato" and I say "potato." But in the end, it is still a
grapefruit, by any other name. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"David Anton" <Da********@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com... Actually, | and & are also logical operators. They are overloaded to
serve
as both bitwise operators and non-short-circuit logical operators
(identical
behavior to the VB And/Or operators).
For a more complete discussion, see
http://www.tangiblesoftwaresolutions...d%20CSharp.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
"Kevin Spencer" wrote:
The difference here is the difference between logical operators and
bitwise
operators.

&& and || are logical operators.
& and | are bitwise operators.

A logical operator is used to evaluate an expression, and returns true or
false. The AND operator indicates that the operands on both sides MUST
BOTH
evaluate to true. The OR operator indicates that only ONE of the operands
must be true (one OR the other)

X == 1 && X ==2 - False always, since X cannot logically be both 1 AND 2.
Y == 1 && Y < 2 - True, if Y is equal to 1, because 1 is logically less
than
2. False if Y is equal to 0, because although 0 is less than 2, 0 is not
logically equal to 1, and thus BOTH can not be true, unless Y is equal to
1.

X < 2 || X > 2 - True, unless X is equal to 2, since 2 is neither less
than
2 nor is it greater than 2
X == Y || X != Y True always. If X equals Y, the first part is true. If X
is
not equal to Y, the second part is true. Therefore, one OR the other will
always be true.

A bitwise operator is actually a mathematical operator that combines 2
binary values to create a third. the way that this is done is that each
bit
in a value is compared with the bit at the same position in another
value,
and depending on the operator used, yeilds a 1 or a 0. The resulting
value
is the value of the resulting binary number.

Bitwise operators are a "bit" more difficult to understand. Here's a good
reference, with examples:

http://en.wikipedia.org/wiki/Bitwise_operation

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
"Dinsdale" <ru********@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
>I would like to compare a string value to a pre-determined list of
> other strings. Is there a simple way to do this in one statements like
> this:
>
> if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
> {}
>
> This syntax doesn't work (and in fact I'm not quite sure what this
> statement really is saying) but I'd like to be able to do *something*
> like this. One suggestion was to add the compare strings to an
> arraylist and do a binary search.
>
> It's sad that my knowledge of and/or operators is so lacking. If anyone
> knows of a good link to explain the difference between &&, &, ||, | i
> would be greatly appreciative. I don't know how I have survived
> without knowing these things but here I am. :-}
>
> Some examples of syntax I don't really understand:
> Example 1:
>
> if ((eventMask & Win32Com.EV_CTS) != 0)
> {
> i |= Win32Com.MS_CTS_ON;
> }
>
> Example 2:
> if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
> Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
> | Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
> Win32Com.EV_ERR))
>
>
> Any info would be helpful. I don't mind doing the heavy lifting, I just
> need a bit of direction.
>
> Cheers,
>
> Russ
>


Jun 12 '06 #17
I don't agree (unless I don't understand..)
| and & serve two purposes:
1. Bitwise operators on integer types.
2. Non-short-circuiting logical operators on bool types.

You can't just pick one - you have to understand that these operators are
overloaded to serve two very different purposes.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
"Kevin Spencer" wrote:
Actually, | and & are also logical operators. They are overloaded to
serve


That's just a matter of which semantics you're using, which is generally
dependent upon what you're doing with them. They are actually, like anything
else in a computer processor, mathematical operators. I found the semantics
I used to be helpful is expressing the difference between their general
usage.

You say "tomato" and I say "potato." But in the end, it is still a
grapefruit, by any other name. ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.

"David Anton" <Da********@discussions.microsoft.com> wrote in message
news:6A**********************************@microsof t.com...
Actually, | and & are also logical operators. They are overloaded to
serve
as both bitwise operators and non-short-circuit logical operators
(identical
behavior to the VB And/Or operators).
For a more complete discussion, see
http://www.tangiblesoftwaresolutions...d%20CSharp.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter
Instant C++: VB to C++ converter
"Kevin Spencer" wrote:
The difference here is the difference between logical operators and
bitwise
operators.

&& and || are logical operators.
& and | are bitwise operators.

A logical operator is used to evaluate an expression, and returns true or
false. The AND operator indicates that the operands on both sides MUST
BOTH
evaluate to true. The OR operator indicates that only ONE of the operands
must be true (one OR the other)

X == 1 && X ==2 - False always, since X cannot logically be both 1 AND 2.
Y == 1 && Y < 2 - True, if Y is equal to 1, because 1 is logically less
than
2. False if Y is equal to 0, because although 0 is less than 2, 0 is not
logically equal to 1, and thus BOTH can not be true, unless Y is equal to
1.

X < 2 || X > 2 - True, unless X is equal to 2, since 2 is neither less
than
2 nor is it greater than 2
X == Y || X != Y True always. If X equals Y, the first part is true. If X
is
not equal to Y, the second part is true. Therefore, one OR the other will
always be true.

A bitwise operator is actually a mathematical operator that combines 2
binary values to create a third. the way that this is done is that each
bit
in a value is compared with the bit at the same position in another
value,
and depending on the operator used, yeilds a 1 or a 0. The resulting
value
is the value of the resulting binary number.

Bitwise operators are a "bit" more difficult to understand. Here's a good
reference, with examples:

http://en.wikipedia.org/wiki/Bitwise_operation

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

A lifetime is made up of
Lots of short moments.
"Dinsdale" <ru********@gmail.com> wrote in message
news:11**********************@j55g2000cwa.googlegr oups.com...
>I would like to compare a string value to a pre-determined list of
> other strings. Is there a simple way to do this in one statements like
> this:
>
> if(strMystring.ToUpper() == ("STRING1"| "STRING2"| "STRINGX"))
> {}
>
> This syntax doesn't work (and in fact I'm not quite sure what this
> statement really is saying) but I'd like to be able to do *something*
> like this. One suggestion was to add the compare strings to an
> arraylist and do a binary search.
>
> It's sad that my knowledge of and/or operators is so lacking. If anyone
> knows of a good link to explain the difference between &&, &, ||, | i
> would be greatly appreciative. I don't know how I have survived
> without knowing these things but here I am. :-}
>
> Some examples of syntax I don't really understand:
> Example 1:
>
> if ((eventMask & Win32Com.EV_CTS) != 0)
> {
> i |= Win32Com.MS_CTS_ON;
> }
>
> Example 2:
> if (!Win32Com.SetCommMask(hPort, Win32Com.EV_RXCHAR |
> Win32Com.EV_TXEMPTY | Win32Com.EV_CTS | Win32Com.EV_DSR
> | Win32Com.EV_BREAK | Win32Com.EV_RLSD | Win32Com.EV_RING |
> Win32Com.EV_ERR))
>
>
> Any info would be helpful. I don't mind doing the heavy lifting, I just
> need a bit of direction.
>
> Cheers,
>
> Russ
>


Jun 16 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
4
by: Ryan Gaffuri | last post by:
I know that this works. I just don't get the syntax. I know its checking the OS. just not sure how it works. var v = navigator.appVersion.toUpperCase() if (1+v.indexOf('WIN98') os =...
2
by: sunny076 | last post by:
Hi, I am confused with the syntax for NOT in MYSQL where clause and wonder if an expert in MYSQL can enlighten me. There are possibly two places NOT can go in: select * from employee_data...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
6
by: T. Wintershoven | last post by:
Hi Can someone please tell me whats wrong with the last line of the query below. The first three lines work fine but when i add the fourth line i get an error message (see text at ERROR...
26
by: Neville Lang | last post by:
Hi all, I am having a memory blank at the moment. I have been writing in C# for a number of years and now need to do something in VB.NET, so forgive me such a primitive question. In C#, I...
14
by: DavidOwens | last post by:
Hi there everybody iv designed a database, curently when the delete button is pushed, it deletes the record completly, but i dont want that i want it to just disable the dispenser. iv crreated,...
12
by: panteraboy | last post by:
Hi there again folks. Ps thanks for all the help gettin me this far. I get an 3075 syntax error (missing operator) in the following code of the click event. The code worked fine before i added the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.