LonkeroAdministrator
(KiX Master Guru)
2005-09-01 01:34 AM
what is this binary not operand?

sorry if I make stupid questions but...
what is the meaning of the ~ operand?

I used it in kixgolf as -1:
Code:
$string = right($string,~)



but surely I do hope this is not it's only usable place.

now.
tried simple things.
like:
Code:
7 ~ 7 ?


crash...

Code:
~7 ?


crash.

I got it to not crash with:
Code:
7 + ~7 ?


but the result of -1 is not making me happy.
I can come up with this simply by:
Code:
7 + -1-7 ?



or similar result:
Code:
+~7 ?


remember, the plus sign is needed. otherwise it will crash.
now the result is -8.

so, conclusion.
~ is nasty crashing -1 which negates every number coming after it.
but, if you don't have anything before it, it will crash kixtart.

what's the use for anything like that?

again, sorry if I'm doing dumb questions but I must wonder...


LonkeroAdministrator
(KiX Master Guru)
2005-09-01 05:05 AM
Re: what is this binary not operand?

oh, and as I wait for someone to explain the usage of this to me I would like to request something.

as this is not usable for NOT (yes, the logical not) operations, could we have "!" as logical not?
"not" is sooo long word to type


Richard H.Administrator
(KiX Supporter)
2005-09-01 11:15 AM
Re: what is this binary not operand?

Simplest way to see what a binary NOT does is to use the Windows Calculator in binary mode.

Simply, it reverses all the "1"s and "0"s. The really important thing to remember is that the size of the data type will determine how many places are reversed - play with the QWORD, DWORD, WORD and BYTE setting to see what I mean.

If you are using NOT in KiXtart you should use the largest data type to hold the number and then use binary AND to ensure that you get the right sized result. You can also force the data type using one of the Cxxx() functions if you know what it will be:

Code:
$iByteSize=&FF
$iWordSize=&FFFF

"Not 100 (BYTE) = " DecToHex((~100) & $iByteSize) ?
"Not 100 (WORD) = " DecToHex((~100) & $iWordSize) ?
"Not 100 (DWORD) = " DecToHex((~CDbl(100))) ?



Richard H.Administrator
(KiX Supporter)
2005-09-01 11:45 AM
Re: what is this binary not operand?

Also, I agree it *is* a bit buggy - I had to use the parentheses to stop the script abending.

As a real world example, say you have a bit-field which is used to hold options and you want to turn one of the bits off without changing the others. You would use NOT to create an inverted bit mask and then use AND to switch the bit off.

The following code switches off bits 1,3 and 5 without affecting the rest of the bits:

Code:
; Bits 1, 3 and 5
$iBitMask=(1 | 4 | 16)

; Inverse bit mask (byte sized)
$iInverse=(~$iBitMask) & &FF

$iNo=99
While $iNo<>""
"Enter a number: " GetS $iNo
If $iNo <>""
$iNo=CInt($iNo)
" " $iNo " with bits 1,3 and 5 reset is: " ($iNo & $iInverse) ? ?
EndIf
Loop



iffy
(Starting to like KiXtart)
2005-09-02 10:15 PM
Re: what is this binary not operand?

And why not use xor? People have been using that for ages.

Richard H.Administrator
(KiX Supporter)
2005-09-05 09:24 AM
Re: what is this binary not operand?

Quote:

And why not use xor?




Because it won't work.

Quote:

People have been using that for ages.




Possibly they have, however they won't have been using it to reset bits in a value. XOR is not the same as a NOT+AND operation

There may be a way to use XOR to reset a bit in a value, but I can't think what it would be off the top of my head or why you would want to use it instead of the simple well known NOT+AND technique. Introducing XOR would make the calculation much more complex.

Here is a couple of simple truth tables with a pair of inputs. You can see where the results differ:
Code:
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0

(~0) & 0 = 0
(~0) & 1 = 1
(~1) & 0 = 0
(~1) & 1 = 0



LonkeroAdministrator
(KiX Master Guru)
2005-09-05 11:35 PM
Re: what is this binary not operand?

in other words, the same result would come from:
(not 0) AND 0 = 0
(not 0) AND 1 = 1
(not 1) AND 0 = 0
(not 1) AND 1 = 0

or even:
1 AND 0 = 0
1 AND 1 = 1
0 AND 0 = 0
0 AND 1 = 0

in other words, the sample wasn't perfect

ruud, when you fix this not operand?


kholm
(Korg Regular)
2005-09-06 02:15 AM
Re: what is this binary not operand?

Richard You are right, XOr won't do it, You wil need the strange construct: Exclusive Not And:
Code:
Function xNotAnd($a,$b)
$xNotAnd = ($a ^ $b) & ($a | $b)
EndFunction



-Erik


LonkeroAdministrator
(KiX Master Guru)
2005-09-06 07:39 AM
Re: what is this binary not operand?

erik...
why your code looks like XOR?

to explane:
Code:

xNotAnd($a,$b)
with:
$a $b = R
0 0 0
0 1 1
1 0   1
1 1 0



Richard H.Administrator
(KiX Supporter)
2005-09-06 10:44 AM
Re: what is this binary not operand?

Quote:

in other words, the sample wasn't perfect



Eh? What do you mean? The sample is fine.

Quote:

ruud, when you fix this not operand?



Apart from abending if you don't protect it with parentheses it is fine. What do you think needs fixing?

Don't make the mistake of assuming that a logical or bitwise operation on a single bit sized datum is the same as a bitwise operation on a collection on bits such as a word or byte.

Did you play with NOT using windows calculator? It would have made things much clearer.

The bitwise "~" is a UNARY operator, like the "-" on negative numbers.

Take the byte (8 bit) value 15. The logical NOT 15 is 0, however the boolean ~15 is 240.
15 = 00001111
~15 = 11110000 = 240

Sample failing code:
Code:
"~15 is " ~15 ?


The above code abends with a "unexpected command" error. Wrapping it in parentheses fixes the problem:
Code:
"~15 is " (~15) ? 



LonkeroAdministrator
(KiX Master Guru)
2005-09-06 11:34 AM
Re: what is this binary not operand?

ja, that parentheses requirement needs fixing.

Ruud van Velsen
(Hey THIS is FUN)
2005-09-06 11:35 AM
Re: what is this binary not operand?

Good catch, this (the parentheses thing) is fixed in the next build.

Ruud


iffy
(Starting to like KiXtart)
2005-09-10 12:32 AM
Re: what is this binary not operand?

I know my bits Richard, did way too much assembly coding when I was a kiddy to ever forget. But complex calculations? Hmm... where is the complexity in the following few lines?

For example, let's reset bit 4 (16)

Code:
$a = 89
if ($a & 16)
$a = $a ^ 16
endif
? $a



Looks very neat and simple to me. But then again, nothing wrong with the ~, as long as it works as expected and when it is nicely documented.
xor is among the fastest (assembly) instructions around

Besides, the XOR is one of the fastest assembly instructions around, it's well known for example that it's quicker to xor a register to 0 then to set it to 0.


Sealeopard
(KiX Master)
2005-09-10 02:17 PM
Re: what is this binary not operand?

Quote:

it's well known for example that it's quicker to xor a register to 0



Yes, for the CS dinosaurs. The current crop of CS students doesn't even know what Assembler is or would be able to program in it.


iffy
(Starting to like KiXtart)
2005-09-10 03:28 PM
Re: what is this binary not operand?

LOL... now you've made me feel old and extinct

Les
(KiX Master)
2005-09-10 03:42 PM
Re: what is this binary not operand?

Quote:

did way too much assembly coding when I was a kiddy




PFFT! Assembly was a luxury that I did not have until later in life. Started out with simple binary and graduated to hex machine language.

Jens is just a young pup... all he knows about machine language and MASM is what he read in a book. He is part of the GUI drag'n'drop generation.


LonkeroAdministrator
(KiX Master Guru)
2005-09-10 10:50 PM
Re: what is this binary not operand?

LOL!

Sealeopard
(KiX Master)
2005-09-11 02:20 PM
Re: what is this binary not operand?

Quote:

Jens is just a young pup



Based on biological ago you might be right, and yes, when I started with my Atari ST back in the mid-80s I did use a GUI (anybody remember the Tramiel Operating System loaded for 3 1/2 inch floppy before one burned it onto EEPROM and booted from that?).
However, I did dabble in Assembler (Motorola 68040 single-board computers) in the mid-90s writing a couple of programs linking multiple SBCs together and wondering why the comm links didn't work until figuring out that the ground-lead had a different enough potential to cause the links to be flaky and that the Assembler code itself was actually correct.


Richard H.Administrator
(KiX Supporter)
2005-09-12 09:59 AM
Re: what is this binary not operand?

Quote:

Looks very neat and simple to me.




Yes, however you've chosen a simple case which makes for simple code

You've also introduced a conditional and branch into something which should be pure bit-wise maths. This is an unnecessary overhead which will only get worse when the data becomes more complex.

Consider a general purpose function uset to reset bits:

Code:
"Simple (one bit case)   : " udfSwitchOffBits(89,16) ?
"Complex (many bit case) : " DecToHex(udfSwitchOffBits(&FFFFFF,&555555)) ?

Function udfSwitchOffBits($iValue,$iOffBits)
$udfSwitchOffBits=$iValue & (~$iOffBits)
EndFunction



I'll be very impressed if you can keep udfSwitchOffBits() anywhere near as simple using XOR. I can smell an impromptu Golf tournament on the wind


Richard H.Administrator
(KiX Supporter)
2005-09-12 10:05 AM
Re: what is this binary not operand?

As a start, here's the best I could do:
Code:
Function udfSwitchOffBits($iValue,$iOffBits)
$udfSwitchOffBits=$iValue ^ ($iValue & $iOffBits)
EndFunction



Which isn't as bad as I thought it'd be.


iffy
(Starting to like KiXtart)
2005-09-12 10:52 AM
Re: what is this binary not operand?

/me could golf that down with short variable names

But, as I said before I have nothing against this not operator but it comes natural to me to use imho a very simple xor construct which accomplished the same. And from the thread it is apperent that the use of the not operator is unclear to people and I assume that the use of xor isn't. Again, as said earlier, perhaps a well written documentation page with a number of well explained examples will clarify this. If not you have something very few people understand and even less people can use correctly. Btw, I fail to see why my code, or your udf for that matter, would become more complex with more complex data?

Edit: when I first replied in this topic I had your 1 line construct in mind but when I pasted my if/then sample I apperently had a caffeine deficiency.