QB64 Phoenix Edition
A quick lesson on: What is IMP? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Official Links (https://qb64phoenix.com/forum/forumdisplay.php?fid=16)
+--- Forum: Learning Resources and Archives (https://qb64phoenix.com/forum/forumdisplay.php?fid=13)
+--- Thread: A quick lesson on: What is IMP? (/showthread.php?tid=305)

Pages: 1 2 3


A quick lesson on: What is IMP? - SMcNeill - 05-02-2022

One way to break down the logic of IMP is to remember with A IMP B:

Your result is *always* going to have all the bits of B set…

For example, let’s assume A and B are both _UNSIGNED _BYTEs.

Now if B =3, the result of A IMP B will be = ??????11, depending on A to fill in the ?
And if B = 5, the result of A IMP B will be = ?????1?1, depending on A to fill in the ?

*Whatever* the final result is, it’s going to have every bit set that B already has set.




And, with that half of the process solved, it’s *also* going to set any bits that A *DOES NOT* have set.

A = 2.  B = 3.

In binary, those are:
A = 00000010
B = 00000011

A IMP B is solved by first setting all the bits in the answer to match B:   ??????11
Then we toggle all the bits in A: 11111101.
And we set the ones that are on, for our answer: 11111111

2 IMP 3 = 255




(NOT A) OR B

That’s the breakdown of what IMP is doing.

(NOT A) says the result is going to have all the bits set that A does NOT have.
OR B says our result is *also* going to have all the bits set that B does.

A IMP B = (NOT A) OR B

Really, that’s all there is to it.  It’s convoluted, and not really something I think most folks ever really need, but that’s all the does in a nutshell.


RE: A quick lesson on: What is IMP? - TarotRedhand - 05-08-2022

IMP is short for Implies. Careful though, this is mathematics not English. Does the program definition language Z still exist? I remember it was actually used in that. In QB not so much. But it's there and if I ever actually need it, I'll use it of course.

TR


RE: A quick lesson on: What is IMP? - PhilOfPerth - 05-09-2022

(05-02-2022, 12:35 AM)SMcNeill Wrote: One way to break down the logic of IMP is to remember with A IMP B:

Your result is *always* going to have all the bits of B set…

For example, let’s assume A and B are both _UNSIGNED _BYTEs.

Now if B =3, the result of A IMP B will be = ??????11, depending on A to fill in the ?
And if B = 5, the result of A IMP B will be = ?????1?1, depending on A to fill in the ?

*Whatever* the final result is, it’s going to have every bit set that B already has set.




And, with that half of the process solved, it’s *also* going to set any bits that A *DOES NOT* have set.

A = 2.  B = 3.

In binary, those are:
A = 00000010
B = 00000011

A IMP B is solved by first setting all the bits in the answer to match B:   ??????11
Then we toggle all the bits in A: 11111101.
And we set the ones that are on, for our answer: 11111111

2 IMP 3 = 255




(NOT A) OR B

That’s the breakdown of what IMP is doing.

(NOT A) says the result is going to have all the bits set that A does NOT have.
OR B says our result is *also* going to have all the bits set that B does.

A IMP B = (NOT A) OR B

Really, that’s all there is to it.  It’s convoluted, and not really something I think most folks ever really need, but that’s all the does in a nutshell.
Then I don't get it! If ANY combinations will give 255, what's the point of it, and how could it be used (if I wanted to)? Can you show a (simple) example?


RE: A quick lesson on: What is IMP? - SMcNeill - 05-09-2022

(05-09-2022, 01:39 AM)PhilOfPerth Wrote: Then I don't get it! If ANY combinations will give 255, what's the point of it, and how could it be used (if I wanted to)? Can you show a (simple) example?

A working demo of IMP, for you:

Code: (Select All)
Dim Man As _Byte, Healthy As _Byte 'our two variables we're going to IMP

Print "Let's determine if you're eligable for a discount for medical insurance."
Print "All women are eligible, as are all healthy men."
Print "Men with pre-existing conditions aren't included in this discount."
Do
    Print
    Print
    Input "Are you a man (Y/N)", Man$
    Input "Are you healthy (Y/N)", Health$
    If InStr(UCase$(Man$), "Y") Then Man = -1 Else Man = 0
    If InStr(UCase$(Health$), "Y") Then Healthy = -1 Else Healthy = 0
    Print Man, Healthy

    Discount = Man Imp Healthy
    If Discount Then Print "You qualify!" Else Print "Unhealthy males don't qualify!"
Loop Until Man$ = ""

Run that a few times and try the various combinations for being a man and being healthy, and remember the rules we set for who qualifies for the insurance discount..

If a man and healthy -- you qualify!
if a man and not healthy -- you don't qualify!
if a woman and healthy -- you qualify!
if a woman and not healthy -- you qualify!

Discount = Man IMP Healthy

As I mentioned in my first post, this is nothing more than a fancy way of writing:  Discount = NOT Man OR Healthy

Swap out that IMP statement for the one above and then run the program again, if you'd like.  Discount = NOT Man OR Healthy runs the exact same as Discount = Man IMP Healthy.


RE: A quick lesson on: What is IMP? - Dimster - 07-27-2022

Thanks for addressing this one Steve

So I gather, the string variables MUST equate to either a -1 or 0 ??? Somewhere in the conditions which set up the IMP, the variables being used in the Implied comparison need to equate to either a -1 or 0. 

A statement like A = 10 : B = 10 : A imp B, doesn't work??


RE: A quick lesson on: What is IMP? - SMcNeill - 07-27-2022

It works, but you may not get the result you're expecting.

Basically, A IMP B is the exact same as NOT A OR B.

In your example case, A = 10, B = 10.
NOT A OR B
NOT 10 OR 10
-11 OR 10
-9

10 IMP 10 = -9, which is TRUE.

Remember, in BASIC, anything non-zero is TRUE by default.


RE: A quick lesson on: What is IMP? - Dimster - 07-27-2022

Where A = 10 : B = 10 : the Math statement of A = B ..  in math, it is a TRUE statement, only if A = B
Where A = 10 : B = 10 : the Logic statement of A IMP B .. in logic, seems it TRUE all the time

Code: (Select All)
FOR x = 1 TO 5
    FOR y = 1 TO 5
        IF x = y THEN z = -1 ELSE z = 0
        IF x IMP y THEN w = -1 ELSE w = 0
        PRINT z, w
    NEXT
NEXT



RE: A quick lesson on: What is IMP? - SMcNeill - 07-27-2022

A IMP B is *always* going to be true if B is non-zero. It's also *always* going to be true if A is anything except negative one.

The *only* time A IMP B is *false* is when A = -1 *AND* B = 0.

Any other combination of values will evaluate to TRUE.

I still hold that it's much less confusing to just write an (IF check for A = -1 AND B = 0 THEN) than it is for (IF A IMP B THEN) statement.


RE: A quick lesson on: What is IMP? - SMcNeill - 07-27-2022

To explain the above, let me break down and simplify the logic for you guys.

Remember A IMP B is the *exact* same thing as NOT A OR B. I'm going to expand this equivalent expression, as everyone has a better grasp on the NOT and OR commands.

Logically speaking, NOT A OR B is basically a truth check to see if either of our two conditions are TRUE, and if so, then we count the statement as TRUE. It's basically IF (NOT A) OR (B) THEN.... <-- I would hope it's easy enough to see the 2 conditions we're checking here.

B .... would be TRUE, as long as it's not 0. That's the basic truth of BASIC -- anything non-zero is counted as TRUE. With (Whatever) OR B, as long as B is TRUE, we evaluate the whole statement as TRUE. B **has** to be zero for the statement to ever be FALSE.

IF B <> 0 THEN the statement is true, regardless of whatever the left side of the statement is. We're checking OR B after all.

For the left side, we're evaluating NOT A. Again, *any* non-zero number would be TRUE. Knowing that, what's the *ONLY* value NOT A can be to become 0??

-1!!

NOT -1 = 0. Every other value equals something else. *ONLY* when A is -1, do we have a FALSE statement of the left side of that OR operator. Any other value is TRUE.

Which brings us to this simple conclusion:

IF A = -1 AND B = 0 THEN
we have a FALSE result
ELSE
everything else is TRUE
END IF

So A = 10... TRUE! Who cares what B is! I can assure you, it's TRUE just from the A side of things...

B = 10? That's got to be TRUE. Doesn't matter what A is, the B side alone defines it as TRUE.

The **ONLY** way you get FALSE is when A = -1 AND B = 0. Any other values than those two and you get TRUE.


RE: A quick lesson on: What is IMP? - Pete - 07-27-2022

@SMcNeill

What about trans-males? IMP that! Big Grin