We want numbers that lie between 1 and 10 inclusive - Page 1
Forum section: electronics / beginners

author: shivajikobardan
author_rank: Contributor
timestamp: November 26, 2021, 03:11:37 pm
content: I know the answer, I need to use an OR gate because that is how the code works. I want to learn the problem solving part of this.The code that works is:Code: (python)if(x<1 or x>10):print("Error")else:print("Ok")Also and gate works if used differently. but I am unable to figure out the problem solving part of it. i.e figuring out how to decide what to use. that is what is confusing me. and this is very common thing in programming. very important as well.I know the truth tables of OR and AND gate as well:https://imgur.com/a/vRp29yxWhat are we expecting in output condition in this case?I will explain it with a figure:(as show in above figure)What is the condition we are expecting there? What is needed to be either true or false in output condition ? Can you clarify that much?This is such a basic question but it is affecting my programming skills too much vividly. I am unable to figure out what condition are we trying to find in output?? I asked this question here as well, but didn't get my answer that I was trying to get.[URL unfurl="true"]https://electronics.stackexchange.com/questions/596563/we-want-numbers-that-lie-between-1-and-10-inclusive-why-should-i-use-or-gate-in/596643#596643

author: rstofer
author_rank: Super Contributor
timestamp: November 26, 2021, 04:44:23 pm
content: I don't think it matters which way you do it but sometimes there is no 'else' clause like:if (some test condition)do something exceptionalend ifIn this case you want to wind up with a true test falling into 'do something'. If there is an 'else' clause then it could go either way. It would still make sense to use a positive test that is easy to understand.FWIW, I would always create my truth tables in binary ascending orderF FF TT FT TI guess an argument could be made for ordering the table by output values but I have never done it. I wait until I get to the Karnaugh Map before I worry about adjacencies.

author: Nominal Animal
author_rank: Super Contributor
timestamp: November 28, 2021, 09:38:24 pm
content: In the arithmetic sense,out = (in ≥ 1 ∧ in ≤ 10)andout = ¬(in < 1 ∨ in > 10)are exactly the same. Here, I'm using∧for OR,∨for AND, and¬for NOT, as is common.Is one of the above more correct than the other? No, both of them are equally exactly correct.But, if you can ONLY use AND (∨) and OR (∧), and cannot use NOT (¬), then you'd be limited to the first one, using OR.I personally don't like it when I'm given such artificial limitations; there should always be a stated reason why, because often there is an even betteroverallsolution that makes the artificial limitation completely irrelevant.Let's examine the truth table by splitting the input into four binary inputs:$$\begin{array}{cc|c}\text{Input} & D C B A & \text{Output} \\\hline0 & 0 0 0 0 & 0 \\1 & 0 0 0 1 & 1 \\2 & 0 0 1 0 & 1 \\3 & 0 0 1 1 & 1 \\4 & 0 1 0 0 & 1 \\5 & 0 1 0 1 & 1 \\6 & 0 1 1 0 & 1 \\7 & 0 1 1 1 & 1 \\8 & 1 0 0 0 & 1 \\9 & 1 0 0 1 & 1 \\10 & 1 0 1 0 & 1 \\11 & 1 0 1 1 & 0 \\12 & 1 1 0 0 & 0 \\13 & 1 1 0 1 & 0 \\14 & 1 1 1 0 & 0 \\15 & 1 1 1 1 & 0 \\\end{array}$$If there are more significant binary digit inputs than the above four, any one of them being one means the output must be zero.The correspondingKarnaugh mapwould be 4×4. For example,$$\begin{array}{c|cccc}~ \diagdown & BA & ~ & ~ & ~ \\DC & 01 & 11 & 10 & 00 \\\hline00 & 1 & 1 & 1 & 0 \\01 & 1 & 1 & 1 & 1 \\11 & 0 & 0 & 0 & 0 \\10 & 1 & 0 & 1 & 1 \\\end{array}$$One way to write this would beout = ¬( (D ∧ C) ∨ (D ∧ B ∧ A) ∨ (¬(D ∨ C ∨ B ∨ A))which uses both AND (∧) and OR (∨) operations, as well as NOT (¬). The(D ∧ C)refers to the entire third row, the(D ∧ B ∧ A)refers to the second column in the third and fourth rows, and the¬(D ∨ C ∨ B ∨ A)refers to the fourth column on the first row. These three cover all zeros in the K-map, so OR'ing them together and inverting the result, yields the output.(There are different methods for solving K-maps, and perhaps this one isn't the one you arrive at using what you have in your proverbial "toolbox". That's okay; I pulled this one way out of my proverbial backside, using my experience and observations in minimizing binary expressions in programming as my "toolkit".)So, as you can see, the underlying question,"how to decide what to use"is more of an optimization problem: you decide what to use, based on what gives you the expression you prefer. How you compare different valid expressions, is up to you.

author: Ian.M
author_rank: Super Contributor
timestamp: November 28, 2021, 10:14:36 pm
content: If you've got a windows PC, you may find Logic Friday useful for wrangling combinatorial logic problems. Its a GUI frontend for theEspresso logic minimizerThe original website's gone to domain parking, but it can still be accessed, including downloading the installer,at the Internet Archive.You can enter logic problems by truth table, logic equations, or as a gate diagram and convert from one representation to the others. It also does minimization, and can output C source for functions to generate each output.

author: IanB
author_rank: Super Contributor
timestamp: November 28, 2021, 10:14:59 pm
content: Quote from: shivajikobardan on November 26, 2021, 03:11:37 pmI think you are confusing yourself by thinking about OR gates or AND gates.If you are programming then you want to be thinking in a natural language like English, so you and other people can read the program like a book. If you read a book, you don't have to solve a problem, you just read it.Similarly, with your problem."I want the number to be between 1 and 10""I want the number to be at least 1, and I want the number to be no more than 10"Let the number be N. Then:"I want N >= 1 and I want N <= 10""I want N >= 1 and N <= 10"Code:if (N >= 1 and N <= 10):print("OK")else:print("Error")Also note thatin Python only, you could write this:Code:if (1 <= N <= 10):print("OK")else:print("Error")But be careful, because this won't work the same way in other languages.