Invert any boolean condition
27 December, 2025
Time to read: 1 mins
Last update: 27 December, 2025
- The negation of
A || Bis!A && !B - The negation of
A && Bis!A || !B
Links
See Boolean algebra, “De Morgan’s laws”: https://en.wikipedia.org/wiki/De_Morgan%27s_laws
An example
An example in C++ from the spdlog library, stdout_sinks-inl.h:
// don't throw to support cases where no console is attached,
// and let the log method to do nothing if (handle_ == INVALID_HANDLE_VALUE).
// throw only if non stdout/stderr target is requested (probably regular file and not console).
if (handle_ == INVALID_HANDLE_VALUE && file != stdout && file != stderr) {
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}We group the 2nd and 3rd conditions into a new one:
if (handle_ == INVALID_HANDLE_VALUE && (file != stdout && file != stderr)) {we apply the rule:
if (handle_ != INVALID_HANDLE_VALUE || !(file != stdout && file != stderr)) {then apply the rule again on the 2nd part to get the inverted condition:
file == stdout || file == stderrfinally, we substitute it above:
if (handle_ != INVALID_HANDLE_VALUE || file == stdout || file == stderr) {So the opposite of “if file is an invalid file and no console is attached” is “if file is a valid file or a console is attached”:
# highlight-start
// throw if handle_ is valid or a console is attached.
if (handle_ != INVALID_HANDLE_VALUE || file == stdout || file == stderr) {
# highlight-end
throw_spdlog_ex("spdlog::stdout_sink_base: _get_osfhandle() failed", errno);
}
Other things to read
Popular
- Reveal animations on scroll with react-spring
- Gatsby background image example
- Extremely fast loading with Gatsby and self-hosted fonts