Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
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.
Posts: 78
Threads: 21
Joined: Apr 2022
Reputation:
5
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
Posts: 456
Threads: 63
Joined: Apr 2022
Reputation:
10
(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?
Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
(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.
Posts: 210
Threads: 25
Joined: Apr 2022
Reputation:
5
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??
Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
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.
Posts: 210
Threads: 25
Joined: Apr 2022
Reputation:
5
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
Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
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.
Posts: 1,507
Threads: 160
Joined: Apr 2022
Reputation:
116
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.
Posts: 1,616
Threads: 157
Joined: Apr 2022
Reputation:
77
@SMcNeill
What about trans-males? IMP that!
|