On Wed, 16 Jan 2008, Mathieu Desnoyers wrote:Incorrect. !0 _is_ necessarily 1. It's how all C logical operators work. If you find a compiler that turns !x into anything but 0/1, you found a compiler for another language than C. It's true that any non-zero value counts as "true", but the that does not mean that a logical operator can return any non-zero value for true. As a return value of the logical operations in C, true is *always* 1. So !, ||, &&, when used as values, will *always* return either 0 or 1 (but when used as part of a conditional, the compiler will often optimize out unnecessary stuff, so the CPU may not actually ever see a 0/1 value, if the value itself was never used, only branched upon). So doing "!cs->base_num" to turn 0->1 and 1->0 is perfectly fine. That's not to say it's necessarily the *best* way. If you *know* that you started with 0/1 in the first place, the best way to flip it tends to be to do (1-x) (or possibly (x^1)). And if you can't guarantee that, !x is probably better than x ? 0 : 1, but you might also decide to use ((x+1)&1) for example. And obviously, the compiler may sometimes surprise you, and if *it* also knows it's always 0/1 (for something like the source being a single-bit bitfield for example), it may end up doing something else than you coded that is equivalent. And the particular choice of operation the compiler chooses may well depend on the code _around_ that sequence. (One reason to potentially prefer (1-x) over (x^1) is that it's often easier to combine a subtraction with other operations, while an xor seldom combines with anything around it) Linus --
| Tarkan Erimer | Re: Dual-Licensing Linux Kernel with GPL V2 and GPL V3 |
| Nigel Cunningham | Re: [PATCH] Remove process freezer from suspend to RAM pathway |
| Paul Mundt | Re: 2.6.22-rc4-mm2 |
| Greg Kroah-Hartman | [PATCH 001/196] Chinese: Add the known_regression URI to the HOWTO |
git: | |
| Arjan van de Ven | Re: [GIT]: Networking |
| Gerrit Renker | [PATCH 27/37] dccp: Integration of dynamic feature activation - part 2 (server side) |
| Jarek Poplawski | Re: [PATCH] pkt_sched: Destroy gen estimators under rtnl_lock(). |
| Natalie Protasevich | [BUG] New Kernel Bugs |
