Language: 
To browser these website, it's necessary to store cookies on your computer.
The cookies contain no personal information, they are required for program control.
  the storage of cookies while browsing this website, on Login and Register.

Author Topic:  If (!x) vs if (x==0)  (Read 149 times)

0 Members and 1 Guest are viewing this topic.

Dolfo

« on: 20, December 2024, 16:47:49 »
So we often use

if (!x)

where

if (x) is if (x!=0)

so (!x) is this

if (!(x!=0)

and thats same like

if (x==0)

Using (!x) is one unnessarry processing of a not.

Why we do this? Why we write "if something is not not 0", instead of checking for "if it is 0"?

::)
« Last Edit: 20, December 2024, 16:51:27 by Dolfo »
Don't believe the shit, you hear in mainstream. Believe your own body. Your body is speaking always the true to you. But you need to understand your body. Hear to your body, not to your ego. And when body is calling to you: "Hey something is wrong!" find the reason(s) for that. Man in White don't go for that, they don't want to heal you. They want earn money and sell you medicine, you should take rest of your life. You are not the patient, you are their customer. Never forget this!

Dolfo

« Reply #1 on: 02, January 2025, 03:42:14 »
Looks like compiler can handle this "human miss behavior". I checked the above logics in compiler explorer how the assembly looks.

So if (!(x!=3)) is compiled same like if (x==3).

Thanks to godbolt.org for their free online tool. But I personally think, this is not a good style to write this "not not" logic and let the compiler clean the check.  ::)
« Last Edit: 02, January 2025, 03:45:52 by Dolfo »
Don't believe the shit, you hear in mainstream. Believe your own body. Your body is speaking always the true to you. But you need to understand your body. Hear to your body, not to your ego. And when body is calling to you: "Hey something is wrong!" find the reason(s) for that. Man in White don't go for that, they don't want to heal you. They want earn money and sell you medicine, you should take rest of your life. You are not the patient, you are their customer. Never forget this!

michtoen

« Reply #2 on: 09, January 2025, 17:17:02 »
Well, if (!(x!=3)) is indead something... *cough* ... perhaps i have to make a code review ? :)

But yes, the C compilers will deal with that easily.
vita est proelium

Tags: