473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

bitwise logical operations

Hello all,

In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then
...
do something here
...
End If

where intValue is for examle 4160 (to satisfy the condition)

if i have a C# consolle application with:
Console.WriteLine(4160 & 64)
I will get 64 as a result - as expected, but how do I write the "If "test
with an integer variable passed in??
writting the following:
if (4160 & 64 == 64)
{ ...
do something here
...
}

I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'

Thanks in advance
Filip
Nov 16 '05 #1
3 6496
"Filip Kratochvil" <fi****@iname.com> wrote in message news:Oi**************@TK2MSFTNGP09.phx.gbl...
In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then : : if (4160 & 64 == 64) : : I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'


Just like in VB.NET, Flip, the associativity of the equality operator
supercedes the logical-AND (in this context, it assumes there are
two terms, 4160 (ie, true) and 64 == 64 (ie, true)).

What is needed is a pair of parenthesis (which will force the & to
get interpreted as a bitwise-AND, rather than a logical-AND.

int someInt = 4160;
if ( ( someInt & 64 ) == 64 ) {
// . . . do something . . .
}

Derek Harmon
Nov 16 '05 #2
Derek
Thanks for the quick reply
Filip

"Derek Harmon" <lo*******@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Filip Kratochvil" <fi****@iname.com> wrote in message

news:Oi**************@TK2MSFTNGP09.phx.gbl...
In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then

: :
if (4160 & 64 == 64)

: :
I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'


Just like in VB.NET, Flip, the associativity of the equality operator
supercedes the logical-AND (in this context, it assumes there are
two terms, 4160 (ie, true) and 64 == 64 (ie, true)).

What is needed is a pair of parenthesis (which will force the & to
get interpreted as a bitwise-AND, rather than a logical-AND.

int someInt = 4160;
if ( ( someInt & 64 ) == 64 ) {
// . . . do something . . .
}

Derek Harmon

Nov 16 '05 #3
What your running into is operator precedence...
Try this and it should work...
if ((int)(number & 64) == 64)

Filip Kratochvil wrote:
Hello all,

In VB6 I'm able to do the following:

If (intValue And 64) = 64 Then
...
do something here
...
End If

where intValue is for examle 4160 (to satisfy the condition)

if i have a C# consolle application with:
Console.WriteLine(4160 & 64)
I will get 64 as a result - as expected, but how do I write the "If "test
with an integer variable passed in??
writting the following:
if (4160 & 64 == 64)
{ ...
do something here
...
}

I keep getting error:
Operator '&' cannot be applied to operands of type 'int' and 'bool'

Thanks in advance
Filip


--
Regards,
Dilip Krishnan
MCAD, MCSD.net
dilipdotnet at apdiya dot com
Nov 16 '05 #4

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

Similar topics

9
by: Michael B. Trausch | last post by:
I have a question regarding bitwise operators, I've been trying to figure this out for about two days now, and I just can't seem to get it. What I'm trying to do is use a variable to hold a bitmask...
7
by: Jerry | last post by:
I want an algorithm that do arithmetic operations(divide,mutiply,add etc.)just using bitwise operators:<<,>>,&,|,^; For example,how "a/10" can be implemented. I just want a hint. Thanks.
3
by: Filip Kratochvil | last post by:
Hello all, In VB6 I'm able to do the following: If (intValue And 64) = 64 Then ... do something here ... End If
8
by: Paul E Collins | last post by:
Suppose I have a few Keys objects: Keys k1 = Keys.V; // V Keys k2 = Keys.Control | Keys.V; // Ctrl+V Keys k3 = Keys.Shift | Keys.J; // Shift+J I need to determine which of these include the...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
1
by: S Shulman | last post by:
Hi I am looking for a sample of code that sets and reads individual bits within an Integer Thank you, Shmuel Shulman SBS Technologies LTD
3
by: shdwsclan | last post by:
I am native to various languages but bitwise operators just kill me. I see how much I take object oriented languages for granted. I like all the other c derivitives but ANSI C is making me loose my...
45
by: Carramba | last post by:
Hi! I now that I can't do straight forward any bitwise operation on float (double etc..). But I wondering what is the easiest/best way to do this? I was thinking if I have float x=1.1111 so I can...
29
by: Carl Banks | last post by:
Anyone with me here? (I know the deadline for P3 PEPs has passed; this is just talk.) Not many people are bit-fiddling these days. One of the main uses of bit fields is flags, but that's not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.