There is a code on C # which is used in Unity3D. Visual Studio does not find any warnings in it, but the compiler in the unit displays a warning:

warning CS0675: The operator | ' used on the sign-extended type int'. Consider casting to a smaller unsigned type first int'. Consider casting to a smaller unsigned type first

A piece of code, which swears:

 long old = _outKey[8] & 0xff; old |= _outKey[9] << 0x08 & 0xff00; old |= _outKey[10] << 0x10 & 0xff0000; old |= _outKey[11] << 0x18 & 0xff000000; 

The problem is in casting. Initially, this code is ported from Java, where everything is fine, and the variable in general was of type int. However, Sharpe does not like such bit operations on signed numbers.

And if you declare a variable as an int, then on the last line, VS swears at the need to cast long to int (why is there 4 bytes ?!).

I also tried to declare uint - it also requires typing types.

Tell me, what data type is better to use?

    2 answers 2

    I found your previous topic, I suggest this option:

     byte[] _inKey = new byte[16]; uint old = (uint)_inKey[8] & (uint)0xff; old |= (uint)_inKey[9] << 8 & (uint)0xff00; old |= (uint)_inKey[10] << 0x10 & (uint)0xff0000; old |= (uint)_inKey[11] << 0x18 & (uint)0xff000000; 

    Long in .NET is always 8 bytes (unlike pluses)

    • Thanks, now began to understand the typing. - Yevgeny Karpov

    The compiler points out to you that your code is potentially a source of a rather serious and elusive error. You are trying to apply the OR operator to an operand that allows negative values, and store the result in a larger variable. For example, if you start performing bitwise operations with a negative operand of the int type, and then save it all in long, you risk getting something irresponsible due to the peculiarities of the negative numbers in the computer’s memory (google according to the phrase "Additional code"). Instead of int, it makes sense for you to use something unsigned, say, uint