473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[c#] long and bitwise & operator

hello,

Anybody can tell me why it is impossible to use the '&' operator
with operands of type 'long' or 'ulong' and what i must use instead
of this?
thanks.

Jan 10 '06 #1
4 10270
Not really sure what you mean, works fine for me using the following test:

public class OrApp
{
public static void Main()
{
long a = 0xF0F0F001;
long b = 0x0B0BF010;
ulong c = 0xF0F0F001;
ulong d = 0x0B0BF010;
System.Console. WriteLine(strin g.Format("{0:X} ",a & b));
System.Console. WriteLine(strin g.Format("{0:X} ",c & d));
}
}

Can you give more info about the kind of error/problem you encounter?

rgds,
Baileys

"se*******@yaho o.fr" wrote:
hello,

Anybody can tell me why it is impossible to use the '&' operator
with operands of type 'long' or 'ulong' and what i must use instead
of this?
thanks.

Jan 10 '06 #2
thanks for the response.

with it, i have been able to find the problem. it was a mistake
from me (i think) because i used long instead of ulong. however,
the error reported by the compiler is relatively strange
(cs0019:Operato r '&' cannot be applied to operands of type 'long' and
'ulong').

here is a sample code :

public class StrangeLongComp ileError {
public static void Main(string[] pArgs) {
long lValue = 0x7f00000000000 000L;
byte lbyte0 = (byte)((lValue & 0xff00000000000 000L) >> 56);
}
}

the code above does not compile because the lValue is a long and the
mask is greater than 0x7f00000000000 000L.

Jan 10 '06 #3
Ah, now i get it (I should have tested my example with more extreme long
values before posting).

Anyway, i think you misinterpret what the compiler is telling you: for
long/ulong (=Int64/UInt64) there are 2 predefined & (binary)-operators:
long operator &(long x, long y);
ulong operator &(ulong x, ulong y);

what you're trying to accomplish is the following:
operator &(long x, ulong y);

your first operand is a long, your second operand is of type ulong (you can
verify this: Console.WriteLi ne(0xff00000000 000000L.GetType ());)

There is no implicit conversion between ulong and long, so the compiler is
telling you that there is no binary &-operator defined that can do what you
ask it to do.

Bottom line: & will work if both operands are long, or both operands are
ulong, but not mixed.

HTH,
Baileys

"se*******@yaho o.fr" wrote:
thanks for the response.

with it, i have been able to find the problem. it was a mistake
from me (i think) because i used long instead of ulong. however,
the error reported by the compiler is relatively strange
(cs0019:Operato r '&' cannot be applied to operands of type 'long' and
'ulong').

here is a sample code :

public class StrangeLongComp ileError {
public static void Main(string[] pArgs) {
long lValue = 0x7f00000000000 000L;
byte lbyte0 = (byte)((lValue & 0xff00000000000 000L) >> 56);
}
}

the code above does not compile because the lValue is a long and the
mask is greater than 0x7f00000000000 000L.

Jan 10 '06 #4
thanks, i understand now.

Jan 11 '06 #5

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

Similar topics

9
2535
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...
12
18624
by: sandy_pt_in | last post by:
How to mulitply two integer numbers using bitwise operators in C language.Please reply as early as possible
2
3469
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those with 2's complement, 1's complement, or sign-magnitude arithmetic. But the followup remark is sometimes also made that the choice of arithmetic isn't completely unconstrained, since the bitwise operators seem to presume a base-2 machine.
9
7030
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...
17
2368
by: zirconx | last post by:
I'm trying to understand how the bitwise AND can be used. I've read about what it does but am having trouble applying it practically. I'm working on a system that somebody else wrote and they make use of a MODE flag that gets passed in. They then compare the mode flag against a hard coded value using bitwise AND, and then show or don't show certain features based on the mode. Example pseudocode: if (mode & 1) do something if (mode...
8
2661
by: DanielJohnson | last post by:
I am reading K&R and right now in section 2.9 bitwise operators. I understood most of the simple things initially but getting stuck with the later part where different functions like getbits(), setbits are explained. Is there a better guide to understand such bit operator problems with some examples. Thanks
5
3295
by: Junmin H. | last post by:
Hello, all. I never use them when I am programming. But i have been reading codes that using them. When should I use them? Why not use char/short/int/long? Is it very important to know them well? Because it's binary, I found that it's a little difficult to understand then decimal. Thanks. Junmin H.
2
1701
by: snorble | last post by:
I started creating a simple "bits" class, intended to act like a array of bits. This was my initial idea, basically just overriding the string representation to display the bitmask (so far): class bits(long): def __str__ (self): s = '' if self == 0L: s += '-' else:
8
8793
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well. BRgds, Daniel.
0
9589
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
9423
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
10049
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
8873
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
7413
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
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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
3565
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.