Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extended KotD #3: _NEGATE
#1
Now this entry should be a fairly short one for you guys, as it's just finishing up the last of our new LOGICAL operations -- _NEGATE.

As you guys should know by now, AND/OR are *binary* operators, while _ANDALSO/_ORELSE are *logical* operators.  The first set does binary math comparison; the second set of commands simply check if values are 0 (FALSE) or not (TRUE).

Along the exact same line as these commands, we have both the old *binary* NOT and the new *logical* _NEGATE.

NOT takes a value and basically flips all the bits in it.  For example, let's look at NOT 1:

00000001  <-- this is the 8-bit representation of 1 that is stored in a byte.
------------  <-- so lets apply NOT to that value here
11111110  <-- and there's the binary representation of what NOT 1 looks like.  

NOT toggles bits for a number.  


Now, as for _NEGATE, what does it do as a *logical* operator??

It returns the opposite of TRUE (-1) and FALSE (0) to us .   A few brief examples would probably be best to clear up any misunderstanding here:

_NEGATE 1 = 0
_NEGATE 0 = -1
_NEGATE 14 = 0
_NEGATE 123.12  = 0

In BASIC, anything non-zero is considered TRUE, while *ONLY* zero is considered FALSE.

Logically, when you _NEGATE anything TRUE, you get back FALSE....  So if you _NEGATE any non-zero value, you will get back a return value of 0.
And if you _NEGATE a false value, you get back TRUE...   In this case, the result is always going to be -1 -- what I guess we could consider to be *ABSOLUTE TRUE*.

NOT -1 = 0 ' NOT TRUE = FALSE  
NOT 0 = -1 ' NOT FALSE = TRUE  

When dealing with NOT, only 0 and -1 have this TRUE/FALSE inverse to them.  Any other values will NOT TRUE = TRUE, as shown below:

NOT 1 = -2 '1 is TRUE, but -2 is also TRUE as it's non-zero
NOT -2 = 1' as above.

_NEGATE doesn't have this issue, as it only returns -1 and 0 as values.  If you logically _NEGATE the truth of something, you end up with FALSE.  

_NEGATE basically:

1) Returns 0 as the result for any non-zero value you negate.
2) Returns -1 as the result, if you send it a value of zero.

It's that simple of a command, at the end of the day.  Wink
Reply
#2
Hi Steve. I believe you may have addressed this for me once before but I'll be damned if I can find it. I do have a vague memory of the negation code which had confused me, I believe is was 

Code: (Select All)
a = 5 * -1
Print a
b = 5 * Not 1
Print b
Print Not 1
As I recall, I couldn't understand why Not 1 was equal to 2?

I was going to try ( using your explanation that I can no longer find) apply the same code to the new _NEGATE to see what the answer to b = 5 * _Negate 1. 

If _NEGATE is logical then _Negate 1 would be zero and not 2, correct?
Reply
#3
(05-10-2024, 03:20 PM)Dimster Wrote: Hi Steve. I believe you may have addressed this for me once before but I'll be damned if I can find it. I do have a vague memory of the negation code which had confused me, I believe is was 

Code: (Select All)
a = 5 * -1
Print a
b = 5 * Not 1
Print b
Print Not 1
As I recall, I couldn't understand why Not 1 was equal to 2?

I was going to try ( using your explanation that I can no longer find) apply the same code to the new _NEGATE to see what the answer to b = 5 * _Negate 1. 

If _NEGATE is logical then _Negate 1 would be zero and not 2, correct?

Code: (Select All)
' v3.13
If _Negate 1 = 0 Then Print "correct"
b = b + ...
Reply
#4
(05-10-2024, 03:20 PM)Dimster Wrote: Hi Steve. I believe you may have addressed this for me once before but I'll be damned if I can find it. I do have a vague memory of the negation code which had confused me, I believe is was 

Code: (Select All)
a = 5 * -1
Print a
b = 5 * Not 1
Print b
Print Not 1
As I recall, I couldn't understand why Not 1 was equal to 2?

I was going to try ( using your explanation that I can no longer find) apply the same code to the new _NEGATE to see what the answer to b = 5 * _Negate 1. 

If _NEGATE is logical then _Negate 1 would be zero and not 2, correct?
NOT 1 is, indeed, -2.   

As I explained, NOT is a bit-flipping tool.   Let's take a moment to look at both the value of 1, and the value of -2, as plain bytes, represented in binary:

1 in binary is  00000001.
-2 in binary is 11111110.


Now, how do we get those binary representations?  Let's break them down for you:

A byte is represented by 8-bits, correct?

Correct!   

But here's a bit of trivia for you -- did you know that only 7 bits are used to hold the value of that byte?  That last bit is reserved specifically for the sign!

01111111 = This is going to be a positive number.
11111111 = This is going to be a negative number.

That last bit on the left denotes if the value is negative.  You can tell by instantly looking at it, to see if it's negative or not.


So if that leftmost bit is just for sign, how do we represent our values?

00000001 <--- this is one, in binary, is it not?
10000010 <--- so wouldn't this make sense to represent -2?  

In many ways, it would.  The problem here is what happens when we "add" those values together:

00000001
10000010 
----------- <-- Add bits 
10000011

Our value now seems to represent -3..... 1 + -2 shouldn't be -3!  Obviously, something is wrong here...

So let's take a step back and try a slightly different representation for a negative number:

00000011 = 3
00000010 = 2
00000001 = 1
00000000 = 0
11111111= -1
11111110 = -2
11111101 = -3

For -1, we set ALL bits to being on.  Then, as our numbers get smaller, we simply toggle one bit after another, just as we would going in the opposite direction.

And let's do the math with these bit representations:

00000001 <-- this is 1, same as always
11111110 <-- and this is what we've decided to make -2
-------------<-- let's add those bits together
11111111   

Look back up just a few lines.  What did we say the value of -1 was, in binary?   11111111

1 + (-2) = -1
00000001 + 11111110 = 11111111


And that's how those values get translated/implemented.  The first bit is always going to tell you if the value is negative.  The rest of the bits are going to represent the value in binary.

If you want a quick shortcut to figure out NOT values with integers, then it's as simple as:

NOT x = - (x + 1)    <-- memorize this simple formula.  It can save you a ton of calculations and quick figuring from time to time.

NOT 1 = - (1 + 1), which is -2
NOT -2 = - (-2 + 1), which is 1
NOT 0 = - (0 + 1), which is -1
NOT -1 = - (-1 + 1), which is 0

NOT 123 = - (123 + 1), which is -124

NOT 3456 = - (3456 + 1), which is -3457

At the end it all, that's basically all NOT is -- it's just a basic math fomula to add one and then negate the result.


Whereas _NEGATE is a logical command.  It only gives TRUE/FALSE back to you as -1 and 0.

_NEGATE 1 = 0
_NEGATE -2 = 0
_NEGATE 0 = -1
_NEGATE -1 = 0

_NEGATE 123 = 0

_NEGATE 3456 = 0

In **ALL** cases, _NEGATE is going to return 0 -- EXCEPT when you negate 0 itself.  Then it's going to return a value of -1.


NOT does math.  (It does add one and then multiple the result by -1.)
_NEGATE just returns True (-1) or False (0).

That's the difference in the two commands.  Wink
Reply




Users browsing this thread: 3 Guest(s)