Page 1 of 2 12>
Topic Options
#146947 - 2005-09-01 01:34 AM what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
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...
_________________________
!

download KiXnet

Top
#146948 - 2005-09-01 05:05 AM Re: what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
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
_________________________
!

download KiXnet

Top
#146949 - 2005-09-01 11:15 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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))) ?


Top
#146950 - 2005-09-01 11:45 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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


Top
#146951 - 2005-09-02 10:15 PM Re: what is this binary not operand?
iffy Offline
Starting to like KiXtart

Registered: 2005-05-29
Posts: 149
Loc: The Netherlands
And why not use xor? People have been using that for ages.
Top
#146952 - 2005-09-05 09:24 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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


Top
#146953 - 2005-09-05 11:35 PM Re: what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
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?
_________________________
!

download KiXnet

Top
#146954 - 2005-09-06 02:15 AM Re: what is this binary not operand?
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
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

Top
#146955 - 2005-09-06 07:39 AM Re: what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
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



Edited by Lonkero (2005-09-06 07:43 AM)
_________________________
!

download KiXnet

Top
#146956 - 2005-09-06 10:44 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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) ? 


Top
#146957 - 2005-09-06 11:34 AM Re: what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
ja, that parentheses requirement needs fixing.
_________________________
!

download KiXnet

Top
#146958 - 2005-09-06 11:35 AM Re: what is this binary not operand?
Ruud van Velsen Moderator Offline
Developer
*****

Registered: 1999-05-06
Posts: 363
Loc: Amsterdam, The Netherlands
Good catch, this (the parentheses thing) is fixed in the next build.

Ruud

Top
#146959 - 2005-09-10 12:32 AM Re: what is this binary not operand?
iffy Offline
Starting to like KiXtart

Registered: 2005-05-29
Posts: 149
Loc: The Netherlands
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.


Edited by iffy (2005-09-10 12:36 AM)

Top
#146960 - 2005-09-10 02:17 PM Re: what is this binary not operand?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11163
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#146961 - 2005-09-10 03:28 PM Re: what is this binary not operand?
iffy Offline
Starting to like KiXtart

Registered: 2005-05-29
Posts: 149
Loc: The Netherlands
LOL... now you've made me feel old and extinct
Top
#146962 - 2005-09-10 03:42 PM Re: what is this binary not operand?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#146963 - 2005-09-10 10:50 PM Re: what is this binary not operand?
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22344
Loc: OK
LOL!
_________________________
!

download KiXnet

Top
#146964 - 2005-09-11 02:20 PM Re: what is this binary not operand?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11163
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#146965 - 2005-09-12 09:59 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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

Top
#146966 - 2005-09-12 10:05 AM Re: what is this binary not operand?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
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.

Top
Page 1 of 2 12>


Moderator:  ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 289 anonymous users online.
Newest Members
Praveer, Ollero, rayyo, Foxlicht, DonChild
17742 Registered Users

Generated in 0.045 seconds in which 0.012 seconds were spent on a total of 13 queries. Zlib compression enabled.