Timeline for Why is the logical NOT operator in C-style languages "!" and not "~~"?
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 28, 2020 at 8:19 | comment | added | Davislor |
@Christophe The reason C uses ! is that B did. B was not designed by the same people who came up with && and || , and they did not realize at the time that the logical operators would need to be extended. Interestingly, B was based on an early version of BCPL, rather than later ones, which used ! rather than * as the dereference operator. C might look very different otherwise!
|
|
Feb 28, 2020 at 0:20 | comment | added | Christophe | Btw there is indeed only a bitwise ^ and no ^^. Why? It would make sense to have a logical xor as well (it‘d be more convenient than (a||b)&&!(a&&b) ). But xor does‘t allow for a short circuit: you always have to evaluate both operands. | |
Feb 28, 2020 at 0:15 | comment | added | Christophe |
According to K&R explanations, the main purpose of short circuit is not to make it faster, but to allow conditions that avoid hard failures (e.g: if (i!=0 && k/i>x) will never lead to a division by 0, which would not be guaranteed without short circuits; same for p==NULL || *p=='\0' ). Then, C before 1978 K&R was not yet C but a development version of it, isn’t it? Finally, I do not get from your explanations why the designers dit chose ! an not ~~ (which was in fact the question that we had to answer). You just say they picked the first. But why?
|
|
Nov 23, 2019 at 19:37 | history | edited | Davislor | CC BY-SA 4.0 |
deleted 21 characters in body
|
Oct 3, 2019 at 20:05 | history | edited | Davislor | CC BY-SA 4.0 |
Reworded discussion about why ~0 != !0.
|
Oct 3, 2019 at 20:00 | history | edited | Davislor | CC BY-SA 4.0 |
Reworded discussion about why ~0 != !0.
|
Oct 3, 2019 at 19:53 | history | edited | Davislor | CC BY-SA 4.0 |
Reworded discussion about why ~0 != !0.
|
Oct 3, 2019 at 19:48 | history | edited | Davislor | CC BY-SA 4.0 |
Added note about binary representations.
|
Oct 3, 2019 at 13:15 | comment | added | Peter Cordes | I think my comment is sufficient, but yeah, maybe a parenthetical (doesn't work for 1's complement or sign/magnitude either) would be a good edit. | |
Oct 3, 2019 at 13:05 | comment | added | Davislor | @PeterCordes You’re absolutely right. I was just focusing on two’s-complement machines because they’re a lot more important. Maybe it’s worth a footnote. | |
Oct 3, 2019 at 10:51 | comment | added | Peter Cordes |
BTW, sign/magnitude and one's complement machines also need separate bitwise vs. logical negation, even if the input is already booleanized. ~0 (all bits set) is one's complement negative zero (or a trap representation). Sign/magnitude ~0 is a negative number with maximum magnitude.
|
|
Oct 1, 2019 at 19:37 | comment | added | Steve | +1. I think this is a pretty good guided tour around the evolution of these operators. | |
Oct 1, 2019 at 18:27 | history | edited | Davislor | CC BY-SA 4.0 |
added 158 characters in body
|
Oct 1, 2019 at 17:53 | history | edited | Davislor | CC BY-SA 4.0 |
added 24 characters in body
|
Oct 1, 2019 at 17:48 | history | edited | Davislor | CC BY-SA 4.0 |
added 24 characters in body
|
Oct 1, 2019 at 17:42 | history | answered | Davislor | CC BY-SA 4.0 |