What does this mean?
Ive just started getting it on loads of stuff!!
Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.js(193,54): UCW0003: WARNING: Bitwise operation ‘|’ on boolean values won’t shortcut. Did you mean ‘||’?
And how do I fix it?
(I of course tried typing || where the | was… but I have no idea…
2 Answers
2
A Bitwise operator creates a new value based on the other two. It is mostly useful in masking operations or things where you have an enum set. The bitwise operation uses a series of || calls on each ‘bit’ of a byte. As the comments say you most likely want the standard ‘or operator’ which is ‘||’.
Explaining bitwise: If you know how to understand binary (1s and 0s) then lets define a pseudo-code enum set with their binary equivalent next to them.
enum colors{
red, 0x00000001
blue, 0x00000010
green, 0x00000100
yellow 0x00001000
}
Lets say you have a target with a color value of orange 0x00001001.
Now you can use your bitwise operator to compare
if(orange == colors.red | colors.yellow){ … }
colors.red | colors.yellow is equivalent to:
0x(0||0)(0||0)(0||0)(0||0)(0||1)(0||0)(0||0)(1||0) = 0x(0)(0)(0)(0)(1)(0)(0)(1) = 0x00001001 = 9
if(orange == colors.red | colors.yellow) is equal to saying if (9 == 9)
colors.red || colors.yellow is equivalent to a decimal calculation:
0x00000001 || 0x00001000 = 1 || 8 = true
orange = 9 in decimal which is also true so if(orange == colors.red || colors.yellow) is equal to saying if (true == true)
Using | when you’re ORing logically means:
if(a | b)
Check “a” AND check “b” and if EITHER ONE is true, return true, otherwise return false.
However, using || when you’re ORing logically means:
if(a || b)
Check “a” and if it’s true, return true (“b” is not checked if this happens). Only if “a” is false, then also check “b” and if it’s true, return true.
It’s called a short-circuit operator.
The problem is that | could also be used for bitwise operations and if a | b creates a bitwise operation, the compiler will treat it as such and you might end up with some pretty strange bugs that you won’t detect as bugs.
Thus, if Unity thinks you really are just logically ORing and not bitwise ORing, then it throws the warning you see.
The fix is simple - use ||
But did using || solve your problem? Because that's what you should normally use.
– UnitraxxAhh.. sorry no it didnt... the error (or rather warning, its just yellow) remained.. But should I do it anyway? This was a Unity Technologies Script, from the standard assets... Are they tidying up? Mark
– markfrancombeshow us the line of code for additional help :p
– LoiusUsing || does solve the problem. Change | to || and re-save.
– Eric5h5It is the actual script from the Character Controller package. It has an issue on one line of Input. I can't really recall but it goes somehow: if(Input.GetSomething | Input.GetSomethingElse){} and obviously it comes as a warning since it is not syntactically wrong but the script still works anyway. As mentioned adding a second | removes the warning.
– fafase