Boolean Logic in C

Boolean Logic in C

Post by tripbeam on Tue Jul 01, 2014 9:02 pm
([msg=81919]see Boolean Logic in C[/msg])

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
Poster
 
Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)


Re: Boolean Logic in C

Post by Tentra on Tue Jul 01, 2014 10:10 pm
([msg=81921]see Re: Boolean Logic in C[/msg])

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.
User avatar
Tentra
Poster
Poster
 
Posts: 161
Joined: Wed Apr 30, 2008 4:52 pm
Blog: View Blog (0)


Re: Boolean Logic in C

Post by tripbeam on Tue Jul 01, 2014 10:43 pm
([msg=81922]see Re: Boolean Logic in C[/msg])

Thank you tentra for explaining it to me
tripbeam
Poster
Poster
 
Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)


Re: Boolean Logic in C

Post by tohanov on Wed Jul 02, 2014 12:17 am
([msg=81923]see Re: Boolean Logic in C[/msg])

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
New User
 
Posts: 1
Joined: Sat Jun 28, 2014 10:20 am
Blog: View Blog (0)


Re: Boolean Logic in C

Post by tgoe on Wed Jul 02, 2014 12:54 am
([msg=81924]see Re: Boolean Logic in C[/msg])

Sloppy shit like this breaks my HTS heart :cry:
User avatar
tgoe
Contributor
Contributor
 
Posts: 715
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Boolean Logic in C

Post by cyberdrain on Wed Jul 02, 2014 4:28 pm
([msg=81934]see Re: Boolean Logic in C[/msg])

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
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Boolean Logic in C

Post by Goatboy on Wed Jul 02, 2014 7:23 pm
([msg=81936]see Re: Boolean Logic in C[/msg])

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.
19JAW6GabFHqe9yD9rr26QL3W3V2pNitbD
User avatar
Goatboy
Expert
Expert
 
Posts: 2863
Joined: Mon Jul 07, 2008 9:35 pm
Blog: View Blog (0)


Re: Boolean Logic in C

Post by cyberdrain on Wed Jul 02, 2014 8:59 pm
([msg=81937]see Re: Boolean Logic in C[/msg])

Wow :o I... I stand corrected. Hope I never have to debug it.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron