Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I test if equal to 1 or not equal to 0?

I was coding here the other day, writing a couple of if statements with integers that are always either 0 or 1 (practically acting as bools). I asked myself:

When testing for positive result, which is better; testing for int == 1 or int != 0?

For example, given an int n, if I want to test if it's true, should I use n == 1 or n != 0?

Is there any difference at all in regards to speed, processing power, etc?

Please ignore the fact that the int may being more/less than 1/0, it is irrelevant and does not occur.

like image 993
Emil Avatar asked Jun 07 '10 20:06

Emil


1 Answers

Human's brain better process statements that don't contain negations, which makes "int == 1" better way.

like image 77
Mirek Pluta Avatar answered Oct 25 '22 00:10

Mirek Pluta