Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logic problem: need better logic for desired thruth table.

2 views
Skip to first unread message

Skybuck Flying

unread,
May 28, 2015, 5:49:55 PM5/28/15
to
Hello,

I was just coding and ran into a little logic problem which is as follows:

There are two booleans/variables which can be either false or true.

The desired thrutle table is:

A = input
B = input
C = output

A B C:
-------
F F T
F T F
T F T
T T T

Surpisingly enough I don't think there is a casual/common operator for this
thruth table.

AND does not apply.
OR does not apply.
XOR does not apply.

So I would need some combined operators to give the desired result.

I tried logic below... but funny enough it failed, now I feel like a noob
lol and share this funny little fail logic with you.

Can you improve/fix the logic ?

This is python code, but this^ logic/thruth table problem basically applies
to any programming language:

# loop has to run if:
# while DesiredResult==True:
# Desired truth table for BotWaitForCooldown and CooldownDetected
# BotWaitForCooldown: CooldownDetected: Desired Result:
# False False True
# False True False
# True False True
# True True True
# desired/suiting logic:
# (BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected))

def TestLogic( BotWaitForCooldown, CooldownDetected ):
return BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected)
# this logic is flawed, please improve logic.

if TestLogic( False, False ) == True:
print "test 1 ok"
else:
print "test 1 failed"

if TestLogic( False, True ) == False:
print "test 2 ok"
else:
print "test 2 failed"

if TestLogic( True, False ) == True:
print "test 3 ok"
else:
print "test 3 failed"

if TestLogic( True, True ) == True:
print "test 4 ok"
else:
print "test 4 failed"

Bye,
Skybuck.

Skybuck Flying

unread,
May 28, 2015, 5:55:04 PM5/28/15
to
I think I have run into this problem before... but solved it with some
seperate if statements.

However in this case/this time I would like to not solve it with if
statements, but simply and/or/not/xor, in other words, boolean operators.

So what would help is a "thruth table to logic" convertor/generator ?!

Anybody know one that is suited for boolean logic/software
programming/programming languages/boolean operations ?

Bye,
Skybuck.

Original posting:

"Skybuck Flying" wrote in message
news:3794b$55678d83$5419aafe$56...@news.ziggo.nl...

Grant Edwards

unread,
May 28, 2015, 6:01:11 PM5/28/15
to
On 2015-05-28, Skybuck Flying <skybu...@hotmail.com> wrote:

> I tried logic below... but funny enough it failed, now I feel like a
> noob lol and share this funny little fail logic with you.
>
> Can you improve/fix the logic ?

> # while DesiredResult==True:
> # Desired truth table for BotWaitForCooldown and CooldownDetected
> # BotWaitForCooldown: CooldownDetected: Desired Result:
> # False False True
> # False True False
> # True False True
> # True True True
> # desired/suiting logic:
> # (BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected))

I don't see why you think that's the desired logic, since it doesn't
match your truth table or your test.

> def TestLogic( BotWaitForCooldown, CooldownDetected ):
> return BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected)
> # this logic is flawed, please improve logic.

def TestLogic( BotWaitForCooldown, CooldownDetected ):
return not ((not BotWaitForCooldown) and CooldownDetected)

works for me...

--
Grant Edwards grant.b.edwards Yow! Alright, you!!
at Imitate a WOUNDED SEAL
gmail.com pleading for a PARKING
SPACE!!

Skybuck Flying

unread,
May 28, 2015, 6:07:32 PM5/28/15
to
This is a start lol:

https://www.youtube.com/watch?v=lKqTSBKmWA4

I wonder if it can be simplied... I'll give it a try.

Basically it comes down to creating a logic expression for each true result
in the desired output and or-ing with each other.

The variables leading to the true result in the desired output need to be
kept if true, and negated if false.

So for example:
A B C
F T T

((NOT A) AND (B) AND ETC) OR ETC.

What the video didn't really explain is probably to "and" the variables...
but it did mention "multiply".

I guess "AND" is the closest thing to a multiply ;)

Makes sense... only AND gives a 1 output if all variables are true,
otherwise it would zero out...

Bye,
Skybuck.

Skybuck Flying

unread,
May 28, 2015, 6:08:47 PM5/28/15
to
However I can already see I am not happy with this video solution.

I have 3 true outputs, and only 1 false output.

That would require a lot of logic.

I guess I can turn it around and negate the whole thing... and focus on the
false output.

Bye,
Skybuck.

Grant Edwards

unread,
May 28, 2015, 6:11:36 PM5/28/15
to
On 2015-05-28, Grant Edwards <inv...@invalid.invalid> wrote:
> On 2015-05-28, Skybuck Flying <skybu...@hotmail.com> wrote:
>
>> I tried logic below... but funny enough it failed, now I feel like a
>> noob lol and share this funny little fail logic with you.
>>
>> Can you improve/fix the logic ?
>
>> # while DesiredResult==True:
>> # Desired truth table for BotWaitForCooldown and CooldownDetected
>> # BotWaitForCooldown: CooldownDetected: Desired Result:
>> # False False True
>> # False True False
>> # True False True
>> # True True True
>> # desired/suiting logic:
>> # (BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected))
>
> I don't see why you think that's the desired logic, since it doesn't
> match your truth table or your test.
>
>> def TestLogic( BotWaitForCooldown, CooldownDetected ):
>> return BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected)
>> # this logic is flawed, please improve logic.
>
> def TestLogic( BotWaitForCooldown, CooldownDetected ):
> return not ((not BotWaitForCooldown) and CooldownDetected)
>
> works for me...

While I think that's the most "obvious" solution and can be verified
by inspection: there's only out input state that is "false", so write an expression
for that one state and invert it.

However, you can apply De Morgan's law to simplify it:

not ((not BotWaitForCooldown) and CooldownDetected)

is same as

(BotWaitForCooldown or (not CooldownDetected))

--
Grant Edwards grant.b.edwards Yow! Do you have exactly
at what I want in a plaid
gmail.com poindexter bar bat??

Skybuck Flying

unread,
May 28, 2015, 6:11:55 PM5/28/15
to
Ok, problem solved for now, it seems:

I used video tutorial method and inverted it for the false case ;)

But anyway... I would not only need a "thruth table to logic/boolean
operations converter" but also a "boolean operations optimizer" ;)

# loop has to run if:
# while DesiredResult==True:
# Desired truth table for BotWaitForCooldown and CooldownDetected
# BotWaitForCooldown: CooldownDetected: Desired Result:
# False False True
# False True False
# True False True
# True True True
# desired/suiting logic:
# (BotWaitForCooldown or ((not BotWaitForCooldown) and CooldownDetected))



def TestLogic( BotWaitForCooldown, CooldownDetected ):
# return BotWaitForCooldown or ((not BotWaitForCooldown) and
CooldownDetected) # this logic is flawed, please improve logic.
return (not ((not BotWaitForCooldown) and CooldownDetected)) # fixes it.

Grant Edwards

unread,
May 28, 2015, 6:13:09 PM5/28/15
to
On 2015-05-28, Skybuck Flying <skybu...@hotmail.com> wrote:

Don't they teach Karnaugh mapping any more?

http://en.wikipedia.org/wiki/Karnaugh_map

--
Grant Edwards grant.b.edwards Yow! I want the presidency
at so bad I can already taste
gmail.com the hors d'oeuvres.

Grant Edwards

unread,
May 28, 2015, 6:15:14 PM5/28/15
to
On 2015-05-28, Grant Edwards <inv...@invalid.invalid> wrote:
> On 2015-05-28, Skybuck Flying <skybu...@hotmail.com> wrote:
>
>> However I can already see I am not happy with this video solution.
>>
>> I have 3 true outputs, and only 1 false output.
>>
>> That would require a lot of logic.
>>
>> I guess I can turn it around and negate the whole thing... and focus on the
>> false output.
>
> Don't they teach Karnaugh mapping any more?
>
> http://en.wikipedia.org/wiki/Karnaugh_map

BTW, your example truth table is shown in the paragraph '2-variable
map examples'

--
Grant Edwards grant.b.edwards Yow! Oh my GOD -- the
at SUN just fell into YANKEE
gmail.com STADIUM!!

Lew Pitcher

unread,
May 28, 2015, 6:39:53 PM5/28/15
to
On Thursday May 28 2015 17:50, in comp.lang.c, "Skybuck Flying"
<skybu...@hotmail.com> wrote:

> Hello,
>
> I was just coding and ran into a little logic problem which is as follows:
>
> There are two booleans/variables which can be either false or true.
>
> The desired thrutle table is:
>
> A = input
> B = input
> C = output
>
> A B C:
> -------
> F F T
> F T F
> T F T
> T T T

Seems simple enough: C == A || !B

18:38 $ cat testlogic.c
#include <stdio.h>
#include <stdlib.h>

/*
** A = input
** B = input
** C = output
**
** A B C:
** -------
** F F T
** F T F
** T F T
** T T T
*/

int testlogic(int a, int b)
{
return (a || !b);
}

int main(void)
{
/* A B C */
int ttable[4][3] = { {0,0,1}, /* F F T */
{0,1,0}, /* F T F */
{1,0,1}, /* T F T */
{1,1,1} /* T T T */
};
int rc = EXIT_SUCCESS;
int i, max;

for (i = 0, max = sizeof(ttable) / sizeof(ttable[0]); i < max ; ++i)
if (testlogic(ttable[i][0],ttable[i][1]) != ttable[i][2])
{
printf("testlogic failed on test %d\n",i);
rc = EXIT_FAILURE;
}

if (rc == EXIT_SUCCESS) puts("SUCCESS");

return rc;
}
18:39 $ cc -o testlogic testlogic.c
18:39 $ ./testlogic
SUCCESS


--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Paul

unread,
May 28, 2015, 7:21:32 PM5/28/15
to
If you ever have a really complicated truth table,
you can use Quine McCluskey minimization. At work, I
had no tool for minimizing boolean equations, so I got
some code from another engineer, code that ran on a
mainframe computer. And I converted the code to run
on a personal computer. The computers were so slow
back then, it might take ten to fifteen minutes to
minimize a ten variable truth table. On the mainframe
you could have a 2MB array for storage, whereas on my
personal computer at the time, the memory was segmented
and required some tricks to get enough.

http://en.wikipedia.org/wiki/Quine%E2%80%93McCluskey_algorithm

If it isn't a scam, the source code should be
reasonably short. The program I was using, might have
been on the order of 120 lines of source. This example
is a bit longer, and the couple programs I've been looking
at the source, I don't recognize what they're doing.

http://sourceforge.net/projects/mini-qmc/files/?source=navbar

Your problem isn't large enough to need this sort
of thing, but I thought I'd throw it in as a topic
of general interest. If it's one thing I've learned
over the years, hand-optimization of boolean equations
frequently leads to errors. And it's when you start
engaging your brain, and saying stuff like "I know the
answer", instead of sticking with your boolean algebra,
that errors creep in.

If you need a test case for your QM code, enter an
"XOR tree", as an XOR tree is irreducible and
should cough out the same info, as you entered in
the first place. That was one of my test cases,
when porting the code maybe 30 years ago. (And
no, I didn't keep a copy of the code. We didn't
do stuff like that back then. I didn't even have
a computer at home back then.)

Paul

Skybuck Flying

unread,
May 28, 2015, 10:44:57 PM5/28/15
to
Interestingly enough the shortest I have seen so far is ***:

def TestLogic( BotWaitForCooldown, CooldownDetected ):

# return BotWaitForCooldown or ((not BotWaitForCooldown) and
CooldownDetected) # this logic is flawed, please improve logic.
# return (not ((not BotWaitForCooldown) and CooldownDetected)) # fixes it.
# return (BotWaitForCooldown or (not CooldownDetected)) # optimization but
looks weird :)
return (BotWaitForCooldown >= CooldownDetected) # *** even shorter, wow
cool, thanks to Jussi Piitulainen

Apperently there is a short-coming/flaw in the reasoning about boolean
logic, if boolean logic is converted to numbers/0/1 and comparisions allowed
then apperently there are even shorter forms ?!? Am I correct ? Or am I
missing something ? Perhaps branching don't count ? Hmmmm..... Is this
branching ? Or something else... hmmm.... I think the comparison result
could be used without requiring branching... so I think my conclusion might
be correct.

Bye,
Skybuck.

"Skybuck Flying" wrote in message
news:7b1ef$556792ab$5419aafe$58...@news.ziggo.nl...

Skybuck Flying

unread,
May 28, 2015, 10:56:03 PM5/28/15
to
I am not so sure anymore about my conclusion, I will investigate this
further tomorrow.

It seems safe to conclude that at least the following operators have their
own thruth tables:

=
<>
>
<
>=
<=

These are the comparision operators.

Assume True is greater than False allows them to be used as well.

The question that remains is:

How many "gates" or "basic" operations or "wires" would they require.

Are some of these perhaps very "efficient" and could thus lead to even
shorter gate designs ?

However for my purposes, reducing code, the answer is already: YES

For software logic/boolean reduction the answer is already YES, funny and
interestingly enough ! ;) :)

Bye,
Skybuck.



M Philbrook

unread,
May 29, 2015, 7:05:24 PM5/29/15
to
In article <3794b$55678d83$5419aafe$56...@news.ziggo.nl>, skybuck2000
@hotmail.com says...
First maintain a bit table, something that your code can reference as
Bools via bit operations..
In your case it's more less simple..

A combine of 3 bits gives you the value of 7 this this can be used as
the OR operation

AND operation.
If value = 7 then.....

OR Operation.

If Value <> 0 then....

Xor Operation,
If Value in [1,2,4] then....

If you need to build the "Value" Product from dangling booleans.

Value := (Value Shl 1) OR Byte(YourBool);

Since the compiler only uses the first bit for native booleans this
works out.

Do the above for all three bools to build a final value..

Or you can simply mask off a bit as your boolean value from a VALUE
location, which makes it faster in the longrun...

Does that do anything for you ?

Jamie


0 new messages