Commit b390fb1
authored
[NFC] Address compiler warnings: C4146 - Use two's complement instead of negation (microsoft#7562)
Replaces uses of the unary - operator on signed integers with the
equivalent (sort of, see the details below) expression '~N + 1',
assigning the result to an unsigned type. This avoids undefined behavior
in edge cases and ensures correctness when certain conditions are met.
Details:
This transformation is valid when:
The signed value N is guaranteed to be negative.
The result is stored in an unsigned type that can represent the full
range of the signed type (e.g., uint64_t for int64_t).
The system uses two's complement representation (as is standard on
modern platforms).
While -N is undefined for the minimum representable value (e.g.,
INT64_MIN), the expression ~N + 1 remains well-defined and yields the
correct bit pattern. Assigning this result to an appropriately sized
unsigned type preserves the intended two's complement interpretation
without triggering undefined behavior.
Addresses microsoft#7561.1 parent 23118b9 commit b390fb1
1 file changed
Lines changed: 20 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
137 | 141 | | |
138 | 142 | | |
139 | | - | |
140 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
141 | 146 | | |
142 | 147 | | |
143 | | - | |
| 148 | + | |
144 | 149 | | |
145 | 150 | | |
146 | 151 | | |
| |||
169 | 174 | | |
170 | 175 | | |
171 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
172 | 181 | | |
173 | 182 | | |
174 | | - | |
175 | | - | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
176 | 186 | | |
177 | 187 | | |
178 | | - | |
| 188 | + | |
179 | 189 | | |
180 | 190 | | |
181 | 191 | | |
| |||
470 | 480 | | |
471 | 481 | | |
472 | 482 | | |
473 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
474 | 487 | | |
475 | 488 | | |
476 | 489 | | |
| |||
0 commit comments