473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create 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 10238
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(string.Format("{0:X}",a & b));
System.Console.WriteLine(string.Format("{0:X}",c & d));
}
}

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

rgds,
Baileys

"se*******@yahoo.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:Operator '&' cannot be applied to operands of type 'long' and
'ulong').

here is a sample code :

public class StrangeLongCompileError {
public static void Main(string[] pArgs) {
long lValue = 0x7f00000000000000L;
byte lbyte0 = (byte)((lValue & 0xff00000000000000L) >> 56);
}
}

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

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.WriteLine(0xff00000000000000L.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*******@yahoo.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:Operator '&' cannot be applied to operands of type 'long' and
'ulong').

here is a sample code :

public class StrangeLongCompileError {
public static void Main(string[] pArgs) {
long lValue = 0x7f00000000000000L;
byte lbyte0 = (byte)((lValue & 0xff00000000000000L) >> 56);
}
}

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

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
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...
12
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
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...
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...
17
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...
8
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(),...
5
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?...
2
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): ...
8
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....
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
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...
0
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,...
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...
1
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: 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?
0
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 ...

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.