From 6029fc813d48d67a9086404718c0d51db8c2d30a Mon Sep 17 00:00:00 2001 From: Kalman Keri Date: Tue, 27 Oct 2015 19:50:12 +0100 Subject: [PATCH] Replacing problematic code example in ES.41 --- CppCoreGuidelines.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 7880874..0382547 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -8412,13 +8412,14 @@ Avoid errors. Readability. Not everyone has the operator table memorized. ##### Example - if (a && b == 1) // OK? - if (a & b == 1) // OK? + const unsigned int flag = 2; + unsigned int a = flag; + + if (a & flag != 0) // bad: means a&(flag != 0) Note: We recommend that programmers know their precedence table for the arithmetic operations, the logical operations, but consider mixing bitwise logical operations with other operators in need of parentheses. - if (a && b == 1) // OK: means a&&(b == 1) - if (a & b == 1) // bad: means (a&b) == 1 + if ((a & flag) != 0) // OK: works as intended ##### Note