## Boolean Logic in C

### Boolean Logic in C

I have been programming now for a month or so in C and it's the only language so far i have been liking compared to java, python etc... I have been having trouble in figuring out how to find the steps to get the answer for this !( 1 || 1 && 0 ) i just need to know how to break it down and figure it out. Thank you
tripbeam
Poster

Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)

### Re: Boolean Logic in C

Assuming you already understand basic logical operators Or, And, and Not...

!( 1 || 1 && 0 )

We start inside the parenthesis left to right. The first expression is 1 || 1, which evaluates to 1.

We've simplified a smaller expression to its result. You can think of the larger expression now as:

!( 1 && 0 )

Continuing inside the parenthesis, the next expression is 1 && 0, which is 0.

Again, replacing the smaller expression in the larger:

!( 0 )

The parenthesis serve no purpose as they only hold a value, not an expression. They can be removed, giving:

!0

"Not zero" or "Not false", which is obviously true. The expression, !( 1 || 1 && 0 ), will always evaluate to true.

Hopefully this helps. Essentially you just work down to the deepest level of parenthesis and start evaluating working to the right and eventually up to the next level of parenthesis.

Tentra
Poster

Posts: 161
Joined: Wed Apr 30, 2008 4:52 pm
Blog: View Blog (0)

### Re: Boolean Logic in C

Thank you tentra for explaining it to me
tripbeam
Poster

Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)

### Re: Boolean Logic in C

You're kinda wrong there, Tentra. The evaluation does start from the left, but the && operator has higher priority than ||. That expression will always evaluate to 0 (false).

!(1 || 1 && 0)

!(1 || 0)

!(1)

0
tohanov
New User

Posts: 1
Joined: Sat Jun 28, 2014 10:20 am
Blog: View Blog (0)

### Re: Boolean Logic in C

Sloppy shit like this breaks my HTS heart

tgoe
Contributor

Posts: 716
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)

### Re: Boolean Logic in C

For readability I was always advised not to use more than simple logic in a statement. If what it does isn't immediately clear, it's bad code and the evaluation should be moved to a new variable. So I was wondering, have any of you ever seen an expression similar to "!(1 || 1 && 0)" in good code? Am I wrong in assuming I should never encounter something like this, apart from training exercises?
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)

### Re: Boolean Logic in C

cyberdrain wrote:Am I wrong in assuming I should never encounter something like this, apart from training exercises?

This is actual C code from the gcc compiler: https://github.com/mirrors/gcc/blob/705 ... 1056-L1110
Assume that everything I say is or could be a lie.

Goatboy
Expert

Posts: 2864
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)

### Re: Boolean Logic in C

Wow I... I stand corrected. Hope I never have to debug it.
Free your mind / Think clearly

cyberdrain
Expert

Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)