05-10-2024, 05:48 PM
(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 wasNOT 1 is, indeed, -2.
As I recall, I couldn't understand why Not 1 was equal to 2?Code: (Select All)a = 5 * -1
Print a
b = 5 * Not 1
Print b
Print Not 1
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?
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.