-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBadCheckOdd.qhelp
More file actions
35 lines (27 loc) · 961 Bytes
/
BadCheckOdd.qhelp
File metadata and controls
35 lines (27 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>This rule finds code that uses
<code>x % 2 == 1</code>
to check whether a number <tt>x</tt> is odd, which does not work for negative numbers.
Applying <code>%</code> to negative numbers produces negative results.
For example, <code>(-5) % 2 </code> equals <code>-1</code>, not <code>1</code>.
As a result, this check incorrectly considers all negative numbers as even.
</p>
</overview>
<recommendation>
<p>Consider using
<code>x % 2 != 0</code> or <code>(x & 1) == 1</code> instead.
</p>
</recommendation>
<references>
<li>
MSDN Library: <a href="https://docs.microsoft.com/en-us/cpp/cpp/multiplicative-operators-and-the-modulus-operator">Multiplicative Operators and the Modulus Operator</a>.
</li>
<li>
Wikipedia: <a href="http://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls">Modulo Operation - Common pitfalls</a>.
</li>
</references>
</qhelp>