0

Is it not a good practice to put long condition in if statement like

if(((FIO2PIN & 0x00001000)>>12))

which will give result as 0/1 at the end in ARM7?

Is that so that I can only check for 0 or 1 in if condition?

For example

if(x!=0)

or

if(x==1)??

indirectly (FIO2PIN & 0x00001000)>>12 will also give some value at the end which might be 0/1 depending on FIO2PIN status right?

Clifford
  • 88,407
  • 13
  • 85
  • 165
YMJ
  • 154
  • 1
  • 2
  • 7

2 Answers2

1

The expression ((FIO2PIN & 0x00001000)>>12) is an integer expression and is implicitly cast to a boolean by the if(...), where zero is false and non-zero is true.

There is nothing wrong with that in the sense that it is entirely unambiguous as far as the compiler and language definition are concerned, but I prefer to use only explicitly boolean expressions in conditional statements - in order to make the intent of the programmer clear. That is easily done by explicitly comparing the result with zero; in this case:

if( ((FIO2PIN & 0x00001000) >> 12) != 0 )

However, the shift is entirely unnecessary in either case, because any non-zero value will be accepted as true (which is why you should always compare with zero - or nothing at all). So:

if( FIO2PIN & 0x00001000 )

or

if( (FIO2PIN & 0x00001000) != 0 )

are equally valid - the latter being my preference.

As mentioned, any non-zero value will be accepted as true while only zero is false, so where x is an integer expression, the test x == 1 is a dangerous one, and you should use x != 0 instead.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • @ Clifford - Even syntax if(!x) will do. I did it that way only!! It checks the condition 'if' is true or false?? if not of x==1 then condition is true else false.. – YMJ Jul 23 '13 at 10:49
  • @YMJ: I am not clear what you are saying or how it differs from what I have said. When `x` is an integer expression `if(!x)` and `if(x==0)` are equivalent as are `if(x)` and `if(x != 0)`, but `if(x == 1)` *is not equivalent to* as `if(x)`. In your specific example it would work, but only because of the right-shift - in the general case it is not safe. There is no need to coerce the value to zero or one, since *any* non-zero integer is regarded as TRUE. So either a) do not compare with anything, or b) compare only to zero - never compare to 1. – Clifford Jul 23 '13 at 14:54
0

The if statement will be true if the expression is non-zero. So shifting right by twelve bits is not necessary in your example. Because (FIO2PIN & 0x00001000) is non-zero whenever ((FIO2PIN & 0x00001000) >> 12) is non-zero. In other words, it doesn't matter which bit is non-zero. The if statement will test true if any bit is non-zero.

In my opinion, using a complex expression within an if statement could be bad practice if the expression is so complex that it is difficult for a developer to understand or maintain. But otherwise, as long as the expression is correct, then the compiler should sort it out and you shouldn't need to worry whether it is too complex for the compiler.

kkrambo
  • 6,643
  • 1
  • 17
  • 30