Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise operation on longs

I trying to compile this code:

Int64 itag = BitConverter.ToInt64(temp, 0);
itag &= 0xFFFFFFFFFFFFFC00;

However this gives me the following error:

Operator '&=' cannot be applied to operands of type 'long' and 'ulong'

How do I do this?

like image 916
atoMerz Avatar asked Dec 16 '22 02:12

atoMerz


2 Answers

See http://msdn.microsoft.com/en-en/library/aa664674%28v=vs.71%29.aspx .

If the literal has no suffix, it has the first of these types in which its value can be represented: int, uint, long, ulong.

You have

0xFFFFFFFFFFFFFC00

but Int64.Max is:

0x7FFFFFFFFFFFFFFF

so long is not big enough and ulong is taken as the type of the literal.

Now you have on the left side a Int64, which is signed, and on the right side you have ulong, however, there is not overload of &= which accepts that combination, which leads to the error.

like image 116
Sebastian Mach Avatar answered Jan 02 '23 01:01

Sebastian Mach


C# uses the smallest fitting type for integer literals and 0xFFFFFFFFFFFFFC00 is too big for long so it's an ulong.

So either convert itag to ulong or 0xFFFFFFFFFFFFFC00 to long (unchecked).

like image 29
Ral Zarek Avatar answered Jan 02 '23 02:01

Ral Zarek