Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flags of Booleans 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,311
US
I ran into something that's almost completely confusing me, and since it seems like a good logic problem when it comes to algorithms, I thought I'd ask here as opposed to a language specific thing (and yes it's something I'm trying to program and no it's not class work).

Anyway the problem:

I have a number of flags which can hold boolean values. For each flag, I can hold true or false. Each flag can also hold "both", which means in logic "true or false". Now the problem involving these flags is this: What is the logic to express boolean logic regarding these flags?

Ordinarily this wouldn't be an issue, but there is an additional restriction: "or" can only be used as a top level condition. Which means:
Code:
a=true and (b=false or b=true)
is invalid, but:
Code:
(a=true and b=false) or (a=true and b=true)
is.

Any ideas?

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
And what programming language can only have OR as a top level condition operator?


HTH,

p5wizard
 
I am not fully understanding your meaning when you say a flag can also be both, this implies a 3 state flag (not necesarily impossible but not boolian)

looking at your code examples though they both simplify down to the same thing

Code:
 A=True

a further example of how these flags are used my help.


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
This relates to boolean algebra as is typically taught to electrical engineers rather than in terms of programming.

Boolean algebra has a set of rules, like commutative and distributive, and identities just like regular algebra, as well as a few others like Demorgans theorem.

In applying these rules, you often treat AND as being similar to multiplication and OR as being like addition. Consequently, the statement becomes like the one below. It is important to remember that these are approximations when computing the algebra.

((a = 1) * (b = 0)) + ((a = 1) * (b = 1))
simplifying
a + a * b (algebraically it is really a * b as the first term is 0)
but a + anything becomes a (boolean identity).
so this equates to a

Really the b = 0 is more of a not B than a '0' algebraic value.


 
And what programming language can only have OR as a top level condition operator?

Actually it's a wierd query language that Microsoft has for Windows Update to pass it options upon search. That's one of the rules laid upon the query parser in their API. What I've been doing is trying to make a query writer. Set the flags, code comes up with the proper statement to send the API, all is well. At least that's the plan.

looking at your code examples though they both simplify down to the same thing

Like I said, it's a wierd thing. To specify options you have to feed Windows Update certain strings in sequence which logically fit what you want. Some of these options are boolean flags, but the case is that you might want both and not one or the other on these boolean flag options.

For example, I might want all installed patches + all not installed patches returned (in other words, everything). So you essentially have to specify "IsInstalled=0 or IsInstalled=1" since it's not logically possible for any update to be both.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Hmm... Windoze Update eh? That IS certainly a "Puzzle for Programmers" ;-)

Can you specify

IsInstalled=*

?

HTH,

p5wizard
 
So you essentially have to specify "IsInstalled=0 or IsInstalled=1"
Why not simply NOT specify anything about IsInstalled ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Given a three-state flag where flag = true, flag = both, flag = false, I would use the not operator.

flag != true ==> flag = false or flag = both
flag != false ==> flag = true or flag = both
flag != true and flag != false ==> flag = both



--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Why not simply NOT specify anything about IsInstalled ?

Because a default of IsInstalled=0 is assumed if you don't otherwise.

Given a three-state flag where flag = true, flag = both, flag = false, I would use the not operator.

No NOT operator.

Reference on Windows Update Search QFR, where this came from.

I think though I figured out a good way to solve this. If I treat all the flags specified "both" as a binary sequence of bits changed by addition accordingly, I should be able to come up with all the necessary permutations necessary to adequately specify the conditions without too many duplicates.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
So, can't you replace this:
IsInstalled=0 or IsInstalled=1
with this ?
IsInstalled>=0
or this ?
IsInstalled<>2

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
You sort-of posted your own solution:

(a=true and b=false) or (a=true and b=true)

I assume your concern is that if there are lots of items like "b" where you really want to include both true and false, the whole "or" construction is going to have a very large number of permutations.

Can you say (a = true) and (b = b)?

Or probably your query won't allow you to query something against itself?
 
I took the "three" logic states to be true, false, or don't care, which can take on either value.
 
Thanks for the thoughts on things. Actually for reading things here and seeing some of the questions asked, I got to thinking and reading the Search document closer and realized that I'm making the requirements much more than they need to be. I figured that if I just covered what was forced to a default and ignored the rest unless directly specified by the user, the problem would end up a lot more simplified and it did turn out that way.

Now I just got to figure out why some of the returned items are inconsistent with the logic I'm attempting.

Anyway, thanks again! Sometimes running a problem by someone really helps to be able to see how it needs to be solved.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
If the true, false, don't care (either true or flase) logic works for you and you can reduce your decision making to a "truth table" where you list all combinations of the input and the (desired) output, then you might want to look at a minimization technique called Karnaugh maps. As long as you have 4 or fewer inputs, they are really easy to reduce your sum-of-product logic equations.
 
Kat
Karnaugh Maps
as lomg as you have 4 or fewer inputs
That is not a hard restriction, I have used Karnaugh maps with as many as 13 inputs in the past.
it does take longer to draw up but is still just as easy to get the final result :)

I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
I would like to learn your technique. Five inputs was as large as I have ever handled with them and much more started looking like a hypercube. With a lot of inputs, Quine-Mccluskey (aka tagged tabulation) may be an easier way to go, especially if you can program the solution.
 
certainly the grid does get quite large (& I only did the large one as an exercise).

they are dificult to draw in the forum but the important thing to remember is the colom/row headings must be grey coded (only one variable changing at a time

for example 6 variable would use 8 col & 8 rows the col headings would be as follows

|-A -A -A -A|+A +A +A +A|
|-B -B|+B +B +B +B|-B -B|
|-C|+C|+C|-C|-C|+C|+C|-C|

and similar for the rows


I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top