473,671 Members | 2,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.WriteLi ne(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 6520
"Filip Kratochvil" <fi****@iname.c om> wrote in message news:Oi******** ******@TK2MSFTN GP09.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******** ********@TK2MSF TNGP09.phx.gbl. ..
"Filip Kratochvil" <fi****@iname.c om> wrote in message

news:Oi******** ******@TK2MSFTN GP09.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.WriteLi ne(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
2528
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 of 'flags' for users on a website. The function below is supposed to be a search function for one of those flags in a particular thing. The idea is that when something calls userId(), it should get to see what type of user flags that are...
7
4508
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
736
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
6933
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 Keys.V element, regardless of any other keys. I know it will be a bitwise comparison, but I can't work out the correct syntax to use.
9
7025
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 from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
10
2168
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
1
1416
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
12489
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 hair....especially ANSI C's bitwise operators..... For reference i come from the (Java/C#/C++) realm and was never forced to use these. Many people dont understand these....I tried to make sense....I know the truth tables...and I can do simple...
45
5280
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 multiple it by 1000 to get 11111 and the preform bitwise like <<2 to get 88888 and then divide by 1000 to go back to float 8.8888. but these seem like "nasty" way to do it. So maybe some of you have great tips? Thank you in advance! L R
29
5941
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 often done in Python because of keyword arguments and dicts, which are lot more versatile. Another major use, talking to hardware, is not something oft done in Python either. It seems like this occasional usage wouldn't justify having built-in...
0
8478
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
8397
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
8919
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8599
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6230
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4225
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
4409
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2052
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.