Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitwise operations evaluating to long

I've been converting some Java code to C# and ran into a little pickle. All the documentation on MSDN suggests that all bitwise operations return the type that is being operated on. See: http://msdn.microsoft.com/en-us/library/aa691307(v=vs.71).aspx and http://msdn.microsoft.com/en-us/library/aa691377(v=vs.71).aspx but what ever I do my intellisense keeps telling me that you "Cannot implicitly convert type 'long' to 'int'." The following line is the one with the issue and to me, all the literals in there look like they evaluate to int's and all the operated types are either int's or uint's. What am I missing? I don't even declare any long variables in my file and all the variables below are of type int. The casting to uint is to preserve the unsigned bit-shift operator of java (>>>)

int t1 = ((s13 << 96 - 66) | ((uint)s12 >> 66 - 64)) ^ ((s13 << 96 - 93) | ((uint)s12 >> 93 - 64));
like image 878
jduncanator Avatar asked Feb 24 '26 17:02

jduncanator


1 Answers

When uint is operated with int the result is long. naturally.

int t1 = (int)((((uint)s13 << 96 - 66) | ((uint)s12 >> 66 - 64)) ^ (((uint)s13 << 96 - 93) | ((uint)s12 >> 93 - 64)));

Note: c# compiler is smart and can deal with constants. so (uint)s | 1 is uint because it can determine that statically.

like image 87
Rohit Avatar answered Feb 26 '26 06:02

Rohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!