hey guys welcome back comp 1113 we're still in unit four boolean algebra this is video number three today we'll be doing the derived boolean operators uh we'll talk a little bit about boolean operators in java and we'll even introduce the topic of boolean operators in databases so let's dive right in here we already are very familiar with the basic boolean operators of and or and not now we can combine those to create some new operators uh the first one is nand nand is simply a combination of and and not i almost wish that its name were and followed by the m because that's exactly what it is if we take a look at its truth table here uh i it takes two inputs x y uh if i and them together we get our usual truth table and is only true when both inputs are true and then we negate this and this final column is the definition of nand basically it's true everywhere except where both inputs are true uh the the gate for this thing is your usual nand gate you know it looks like the letter d and then we put a circle attached to the front of it to indicate that we're doing the not operation so the the first part of the picture here is means and and then the circle means not and it's equivalent to uh well this using the the basic boolean operators we can write it using you know the word nand or we can use uh an up arrow to indicate an and you know for shorthand notation so there's no real need to have a nand operator because we already have and and not but we'll see later when we're designing circuits that the nand operator is an is nice for making things a little simpler the next derived boolean operator is nor it's or followed by not so i started with the truth table for for or and then i added a column here where i nodded the or operation so we have basically it's just it's the opposite of or the gate looks like an or gate with a circle on the end to indicate the knot remember the or gate had sort of that curved fin in the back we need to start getting familiar with these gates as we're getting close to uh drawing some logical circuits so start paying attention to these things as we're going through other notation is just to write the word nor and a down arrow this one's harder i have a hard time remembering which one is up arrow and which one is down arrow that's why i think this using the word is is quite common because i think i'm not alone i think a lot of people have trouble remembering which is which so when you use the word there's no confusion even though it's you know a pain to have to write that much perhaps the next derived boolean operator is exclusive or when we learned or we learned that it was defined as one or the other or both uh exclusive or means it removes the or both part of that sentence so the truth table for this thing is uh if if one part of it is true that it's true so uh false or false xor false is false sometimes people pronounce as or uh false or true is true true or false is true but here's the weird case when both inputs are true the xor will output false so here's the gate it's an or gate with an extra fin on the back so it's not too hard to remember all these things the symbol for exclusive or is the typical or plus sign but we just put a circle around it and that's pretty standard notation but oftentimes you will see people write xor the next one is x nor uh i don't really hear people call that xnor that often it's more commonly referred to as the equivalence relation it's basically the opposite of xor so if you look at our truth table i included the truth table for xor and then i knotted all of these values and that gives me the equivalence basically whenever your inputs are the same this thing outputs a one so in the first row here zero and zero they match so it outputs a one in the last row we have one input with one well they match so we output a one the x nor gate is just an xor gate with a not on the end so we can write it in terms of xor x x or y negated or we can write it using the word or it has its own symbol as well so these are well to get familiar i will use these things you know interchangeably so you'll have to know them well enough to you know know what i'm talking about if i just start you know randomly choosing the last version over the middle version say so those are the derived operators but i mean technically we don't need them because they can all be accomplished with the you know traditional and or and not but they do help us simplify things in certain situations often we use boolean you know our logical statements when we're coding you know certainly in an if statement if something is true then you do something yeah so i'm just wrote this in sort of a pseudo code in in boolean or in java the operators are defined as we use this a single pipe for the or operator sometimes this is called the bitwise or we use an ampersand for and we use a carrot for xor exclamation point for not and then we have some some other ones where we have two pipes which we sometimes call the short circuit or it's probably the maybe i should maybe call that the this is more the the boolean or and the other one is the bitwise or and we'll explain the difference between those in a second we also have the boolean and and then obviously we have equality not equal to our comparators uh doing a little example here you know here's a statement you know if it's raining or and then i have stuff in brackets here there's rain in the forecast and it's not the case that it's raining and not cloudy then you know we'll stay inside so if you have a statement like this oftentimes it makes sense for you when you're creating the statement to write it this way like as you're thinking about it you should go oh yeah reigning or this or and that or not this for example you that makes perfect sense to you initially but if this statement has to get evaluated you know a thousand times or a hundred thousand times or a million times during the process of your you know program you might want this to be you know optimized what's the simplest form that this could be uh one it will evaluate faster and two it might make it more readable for someone else so if we convert this thing to boolean variables you know let raining be represented by r uh rain in the forecast bf and clouds bc we can rewrite our statement i wrote it in terms of you know i replaced these the syntax from java back into logic and now we could do that symbolically we could replace the or with our usual plus and we could replace the nots with you know our primes this statement here we can now use our boolean theorems to simplify this thing and then we can rewrite our code to be more efficient so an example we could do de morgan on the first step here move the knot in change the ant or you know fix this double negation maybe do some distributing i probably shouldn't have been so lazy i should be listing the theorems i'm going to require that you always do it and now i'm sort of being a hypocrite and not listing the theorems as i'm doing the algebra there's an absorption theorem here r plus f r naught is just r plus f and then we can do an absorption with this piece uh f or f and c is just f we took this rather complicated initial expression and came up with a much simpler version and then we could rewrite the code and you know come up with a much quicker to evaluate statement than we had before you know what rain and forecast could be some actually in this case it's just a boolean variable so these things would evaluate pretty fast but sometimes you could have uh boolean methods that you know they're complicated and they take a long time to process so you wouldn't want to have to do a big complicated thing here and then do another big complicated thing here and another big complicated thing here when it could be simplified to this i mentioned that bitwise versus the short circuit version of orr an and on the previous page uh when you use a single pipe this is an a bit wise or basically when you have a single one it will always evaluate both pieces regardless of what happens so we know that an or is going to be true if any part of it is true for example like if this first piece is true we don't really need to evaluate the second piece and this both pieces are methods so they might take some time to run so if we can avoid running one altogether we'd like that but in the the current example as it's written it will run both of these before it makes its uh decision and then the short circuit version it's called that because if the first part of it is enough to give you the conclusion then it is over so like in this case an or is true if the first part is true so if it's raining it doesn't need to look at the forecast because this first piece is true and true or anything is true so obviously using the double pipes is preferred uh because it's it will be faster there are times where you might want to use the the single uh over the double particularly if you're just doing bitwise manipulation you can you can compare basically compare two strings with this single or uh if i have a string of zeros and ones and i compare it to another string of zeros or ones with four it basically will just figure out which number is bigger here's an example of a short circuit versus non-short circuit with the and operator i wrote a function here if x doesn't equal zero and one over x is greater than one then you know print hello world um if x is actually a zero i'm in all kinds of trouble with this because i'm using a single ampersand which means it will evaluate both pieces no matter what so when x is a zero sure this part will end up being false and false and anything should be false however when it evaluates the second piece it's going to divide by zero and i'm going to get an error so here's another example of where you'd you know prefer to do the double ampersand because if it evaluates the first term here when x is zero this will be false and then it doesn't need to evaluate the other part because if as soon as you hit a false in an and statement you know the whole thing is false so uh it's just things to keep in mind when you're you know programming uh i was thinking maybe maybe i should do a quick example there of you know of or in and you know if i have int a equals two and b equals say seven you know they're positive integers in memory it doesn't even matter how many bits there you know the last few bits will be for the number two you know say zero zero one zero and the the the last few bits the last few bits of b will be 0 1 1 1 for 7. if i say a or b it with a single pipe it allows you to compare these things bitwise so it does column by column so zero or one is one one or one is one zero or one is one zero or zero is zero so basically this or command helps you figure out which one is bigger because it will always just output the bigger number is that always true zero one zero kind of going on the fly here oh that's not true because if i had something like this suppose i have the number two and the number one it would output the number three so i guess i can't say that it always outputs the bigger number but it does compare them uh using that that the or column by column and and works if i say a and b i'd be anding these two strings together so basically column by column you know zero and one is zero one and one is one zero and one is zero one zero and zero is 0. so it's just checking so i didn't get that right this should be 0. it's just seeing where they're this you know both inputs are one one and one is one so we can use these as you know sort of string operators in you know binary i guess that's why they're called bitwise operators so there we go off on the a bit of a tangent there but you can play around with those you know write a little java program and play around with those operators and see how they behave in database stuff you know sql for example a lot of times you have a sort of a a third option um you know where age is less than 45 do something or gather all the files where age is less than 45 well sometimes age is smaller than 45 sure and sometimes age is greater than or equal to 45 sure but oftentimes in databases you'll have fields that are empty and then we'd have to handle a sort of a third case what happens when age is blank you know is blank what how do you evaluate blank less than 45 becomes sort of an issue so we call this ternary logic where you have sort of three outputs it could be a statement could be true it could be false or it could be sort of the i don't know and that null case is often because you didn't have data so our truth tables become a whole new thing you know we now have a third possibility we still have operators of and or not sometimes they have others but those are the obviously the basics here's a truth table for not in you know a database situation you know here's the obviously the opposite of false is true and the opposite of true is false but what's the opposite of i don't know you can't suddenly know so i guess the opposite is still i don't know if i didn't know what it was before i still don't know what the opposite of i don't know is so it's kind of interesting um the and operator the the beginning i i put all the the usual d's and f's but then i don't know anded with true is i still don't know true ended with i don't know is i don't know um i don't know anded with false oh there we get a false because we know anything added with false is false from above anything ended with false gave us a false even i don't know but true ended with i don't know is i don't know because if the i don't know we're a false well this would be false but if i don't know is it true well then this would be true so it's ambiguous so it's kind of interesting to you know look at how or and and are defined in those situations if you you know ever end up in you know messing around with databases you'll keep this in mind as to how it handles the i don't know cases for or we know orders are true if any part of it is true so true or i don't know is true but when we go false with i don't know we don't know so they actually do make sense uh i'm not gonna going to test you on this i just wanted to make you aware that you know logic applies to you know a bunch of different things in in your field so just making it a you know a topic of of interest i guess uh that's pretty much it for this you know this this unit we continue with boolean in the next unit so we aren't done with this yet uh but this is a good place a natural break so let's end it here and i will see you all in the next one