Search Unity

Resolved Bit operator

Discussion in 'Scripting' started by RobertOne, Feb 16, 2023.

  1. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    Hey, I am getting a warning in my script and i am not sure what it is trying to tell me. this is the script part:

    the error is about these 2 lines:

    Code (CSharp):
    1.  
    2.              num2 |= (flag ? 2 : 0);
    3.             num2 |= (flag2 ? 1 : 0);
    4.  
    and the error message is:

    Code (CSharp):
    1. warning CS0675: Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
    2.  
    can anybody tell me what i did wrong and how to fix that?
    thanks!
     
    Last edited: Feb 16, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    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.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    Well, num2 is a long while your literals are ints. Use this instead:

    Code (CSharp):
    1. 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.
     
    RobertOne likes this.
  4. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    thanks a lot :)
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    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.
     
  6. RobertOne

    RobertOne

    Joined:
    Feb 5, 2014
    Posts:
    259
    ah, good to know. found that snippet on stackoverflow - so, cloud be
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,005
    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 :)