Compiler Warning (level 3) CS0675 | Microsoft Learn
The compiler implicitly widened and sign-extended a variable, and then used the resulting value in a bitwise OR operation. This can result in unexpected behavior.
Try using uint instead of int (same with ulong vs long). The compiler is getting a bit confused about one of the values possibly being negative.
Well, num2 is a long while your literals are ints. Use this instead:
num2 |= (flag ? 2L : 0L);
The issue here is that “signed” variables have their most significant bit represent the sign. So doing a bitwise or between signed values of different bitlength can result in strange effects as the sign bit of a 32 bit integer is in the middle of a 64 bit integer. So just make sure the two operands have the same type / bit size, in your case a long.
ps: Since your variables are named num, num2, num3, num4, is it possible that you grabbed a decompiled codesnippet from somewhere? This naming scheme is common for decompilers like ILSpy. Same is true for the explicit temp variables flag and flag2. Noone would write this method with such names.
Though the whole method looks a bit strange since it uses member variables like the “wasPressedThisFrame” to communicate call related information. The method is most likely supposed to store the “down” and “changed” bit for a particular key at a certain bit offset in a long (so storing 2 bits at once). However such a method should take the actual state that should be stored as an argument. Abusing member variables of the containing class as an argument storage is really bad design. I would guess this is about networking stuff and packing user input into a bitfield!? Well it doesn’t really matter. Without more context it’s hard to reason about the code. Though what we have seen so far looks strange