-3

I have an integer called "count" which increments when a certain condition is met. So I wanted to ask what happens if you write this condition:

if(count % 2)
{
    return even_bit; 
}
else
{
    return odd_bit;
}

The question is basically asking if the if condition above is checking for the condition count%2 = 0 or count%2 !=0 when you don't explicitly define it in the expression for integer data type variables.

Lira
  • 21
  • 3

2 Answers2

3

If an expression evaluates to 0, it will be interpreted as FALSE - for any non-zero value, it will be interpreted as TRUE. In other words,

if(count % 2)

is equivalent to

if(count % 2 != 0)

So your code is "backwards". If the statement is true, count is odd and you most likely would want to return the odd_bit (just guessing here.).

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Which is, of course, why you would never actually want to write it the way he did. If the expression has type `bool`, then it's fine (and in fact any further comparisons, e.g. to `true` or to `false` would be confusing). If it's not `bool`, you don't count on a somewhat obscure and confusing implicit conversion, you write the conversion you want. – James Kanze Oct 16 '13 at 17:38
  • @JamesKanze It's a matter of opinion. I usually prefer to avoid unnecessary comparison and *especially* when it comes to the % operator. The result of % operator is the remainder. The boolean evaluation of that result answers a question *whether* there is some remainder. – Pavel Šimerda Oct 16 '13 at 18:04
  • @JamesKanze Many C programmers are used to common idioms for other operators as well, e.g. `if (flag & MY_FLAG_NAME)` is also usually preferable in comparison with `if (flag & MY_FLAG_NAME == MY_FLAG_NAME)`, etc. – Pavel Šimerda Oct 16 '13 at 18:05
  • Thanks very much. That answered my question. it is the even parity bit i am setting, which is one when there are odd number of ones. – Lira Oct 16 '13 at 18:27
  • Glad it helped. I hope you do realize that this function does **not** return the parity bit of `count`. You would have to do something a bit more advanced for that. See for example http://stackoverflow.com/questions/19373034/how-to-judge-if-there-is-even-1s-in-a-numbers-binary-representation-using-c/19373277#19373277 for a method (and several explanations, including my own brilliant one... :-) ) – Floris Oct 16 '13 at 18:56
  • @PavelŠimerda I don't think its so much a matter of opinion, but rather hiding important information (in other words, obfuscation). There's an implicit conversion to `bool`, and implicit conversions generally make the code less readable. `if` selects not on whether there is a remainder or not, but on whether the expression evaluates true or not. Using some self invented other definition, like "there is a remainer" is obfuscation, whether it works or not. – James Kanze Oct 17 '13 at 08:23
  • @PavelŠimerda Many C programmers have bad habits. Even back when I was learning C (in the 1980's), we knew enough to write `if ( (flag & MY_FLAG_NAME) != 0 )`. (Although I know a few exceptions, I suspect that most people who wrote good C have moved on to C++. There's really no reason to use C today.) – James Kanze Oct 17 '13 at 08:25
  • @JamesKanze Yes and there have always been programmers who thought their habits are the good ones and everyone else is wrong. Religion and technology don't fit well together. – Pavel Šimerda Oct 17 '13 at 13:45
  • @PavelŠimerda It's not a question of religion or habits. It's a question of expressing oneself clearly or not. – James Kanze Oct 17 '13 at 16:01
  • I am with James on this one. Clarity trumps compactness in code every time - a few more characters typed today saves a ton of pain tomorrow. Yay for `-Wall -pedantic` flags. – Floris Oct 17 '13 at 16:29
  • @Floris I respect other people's feelings and opinions even when they aren't the same as mine. I personally consider `if (flag & MY_FLAG_NAME)` much more readable than `if ( (flag & MY_FLAG_NAME) != 0)` and that's a very basic one. Once the lines get longer and more complicated, e.g. a `== 0` at the and looks like a `no` at the and of a sentence reversing the whole meaning. But that doesn't mean I want to turn it into religion. – Pavel Šimerda Oct 17 '13 at 17:56
0
if(count % 2)  

is equivalent to

if(count % 2 != 0)  

Now it will all depend on count whether it is even or odd. In case of even, count%2 is FALSE (return odd_bit) and if count is odd then count%2 is TRUE (return even_bit).

haccks
  • 104,019
  • 25
  • 176
  • 264