4 ms·
Is there an intuition for this correspondence otherwise I don't think it's very helpful
by dananabread 3y ago
Is there an intuition for this correspondence otherwise I don't think it's very helpful
- dataflow 3y agoa * b * c is only nonzero if all of a, b, c are nonzero. That's AND, and should be pretty intuitive. a + b + c is nonzero if any of them are nonzero. (Remember each value is either 0 or 1.) So that's the intuition for OR.
- explaininjs 3y agoa+b+c is nonzero if any of a or b or c are nonzero.
- dataflow 3y agoYeah I just added that. I was hesitant initially cause you have to also note they're nonnegative, and that you're treating them like real numbers rather than mod 2.
- explaininjs 3y agoNatural numbers are the natural numbers for me! (ISO 80000-2, of course)
- naitgacem 3y agothis is the notation used in the chapters about boolean algebra in my digital design course. I think it's pretty neat. I honestly never looked into operator precedence in any language enough to notice the relation.
- aragonite 3y agoConjunction used to be called logical product, and disjunction logical sum, for the reason the other commenters pointed out.
- nicklecompte 3y agoIMO the intuition is to not use any intuition at all: there aren't built-in booleans in C, true is a #define for 1 and false is a #define for 0. For C conditionals, 0 = false, nonzero = true. So a+b != 0 <=> a!=0 or b!=0 a*b != 0 <=> a!=0 and b!=0 Of course this intuition also reveals the pitfall behind this correspondence! You'd better make sure those are unsigned ints or #defined booleans, so you're not using general C expressions. 1 || -1 is true but 1 + (-1) is false. Edit: forgot to mention: INT_MAX || 1 is true, but what about INT_MAX + 1 :)
- vzaliva 3y ago"true is a #define for 1" is bad idea. Because when `x` is, say, `2` then `if x` is not the same as `if x==true`.
- BenjiWiebe 3y agoThat's just the way C is. If you want to check "truthiness" (as much as you can in C), just do 'if x'.
- trealira 3y agoSomething nice in C is that "if (x)" is always equivalent to "if (x != 0)". NULL is a macro for 0, and so is false. Boolean expressions evaluate to 0 if they don't hold. This isn't true in C++, though.
- ahoka 3y agoThen what is _Bool, if not a built in boolean type?
- nicklecompte 3y agoI genuinely forgot stdbool.h existed, I don't actually write that much C myself. My point was more around the conditionals being weakly typed around unsigned ints rather than a specific lack of built-ins. A lot of commenters were going into arithmetic mod 2 or philosophical issues, neither of which actually apply here.
- rantingdemon 3y agoKudos to the people that responded to explain why the correspondence is helpful.
- fasterik 3y agoI always remember it in terms of set operations. && corresponds to set intersection, while || corresponds to set union. The union operation is similar to "adding" two sets together. You also have the distributive property: a && (b || c) == (a && b) || (a && c). The analogy isn't perfect, because || is also distributive over &&, but addition isn't distributive over multiplication. I think this is actually one of the essential properties that distinguishes a Boolean algebra from a ring. Someone with more knowledge of abstract algebra could probably provide more insight here, though.
- layer8 3y agoThere is: The neutral element for + is 0 (x + 0 = x for any x). The neutral element for * is 1 (x * 1 = x for any x). Furthermore, you have arithmetic properties like x * 0 = 0 for any x (annulation) or (x + y) * z = (x * z) + (y * z) for any x, y, z (distributivity). Similarly: The neutral element for OR is false (x OR false = x for any x). The neutral element for AND is true (x AND true = x for any x). Furthermore, x AND false = false for any x, and (x OR y) AND z = (x AND z) OR (y AND z) for any x, y, z. So OR works very much like + algebraically, and AND works very much like *. When using 0 and 1 for false and true, AND is exactly the same as multiplication, and OR is like addition with saturation arithmetics (i.e. 1 + 1 = 1). The common precedence rules stem from those parallels.
- threatofrain 3y agoI've never heard of saturation arithmetic and now it all makes sense. Otherwise I thought it was more common to think of XOR as boolean addition, and OR would be represented as xy + x + y.
- layer8 3y agoYes, true and false with AND and XOR form a mathematical ring [0]. Still, OR is also an additive operation. IMO one could give OR and XOR the same precedence. On the other hand, there is no strict need to have a dedicated boolean XOR operator, as it works the same as = (equals). [0] https://en.wikipedia.org/wiki/Ring_(mathematics) https://en.wikipedia.org/wiki/Ring_(mathematics)
- orlp 3y agoMore than just a ring, it is the simplest finite field.
- o11c 3y agoSaturation is actually the wrong way to think about bools when other operations get involved: saturate(0 - 1) = 0 bool(0 - 1) = 1
- 3y ago
- artsi0m 3y agoYes, sometimes logical AND called logical multiplication and logical OR called logical summation. It seems clear for me, because I remember learning De Morgan's Laws in electronics class and from one specific level of Turing Complete game.