By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,423 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,423 IT Pros & Developers. It's quick & easy.

[c#] long and bitwise & operator

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
thanks, i understand now.

Jan 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.