Anyway I just have a quick question about the following two lines:
if(!(var1 & var2))
...and...
if(var1 | var2)
Are these effectively the same? Or am I getting confused?
I'm trying to get good enough with bitwise operators that I can use them without thinking about it, but it's gonna take some practice :)
Anyway I just have a quick question about the following two lines:
if(!(var1 & var2))
...and...
if(var1 | var2)
Are these effectively the same? Or am I getting confused? |
a--b--(a & b)-!(a & b)--(a | b)
0--0-->--0--------1--------0 1--0-->--0--------1--------1 0--1-->--0--------1--------1 1--1-->--1--------0--------1 (a | b) isn't always !(a & b) |
In response to Stephen001
|
|
Just curious why you are using ==0 instead of !var I assumes you are just for illustration purposes?
|
Because I'm not programming DM, I'm talking algebra. == being the identity operator in most textual representations. It needs to be 0, not something equal to zero ideally .
You'd probably quite happily express things in the form of var1 == 0 anyway when handling bitwise operations in DM. Short-cutting to !var1 affords you no type-safety in this case. You cannot perform bitwise operations on null, or an object reference, or text for example, your data must be an integer or the bitwise operation will not work. Given it's such (or you'd have all kinds of runtime exceptions or side-effect results), testing for numeric zero is fine. The counter-case being if they were lists .... but then you're not doing bitwise operations P: You're doing set intersection. |
In response to Stephen001
|
|
Well that makes sense :) thanks!
|
In the first example, if any of the bits in var1 AND their corresponding bit in var2 are "on". In the case where var1 == 0 and var2 == 0 in the first example, the result of !(var & var2) will be true, not false, like in the second example.
They are not logically equivalent expressions.