#146949 - 2005-09-01 11:15 AM
Re: what is this binary not operand?
|
Richard H.
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
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
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
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
|
|
|
|
#146954 - 2005-09-06 02:15 AM
Re: what is this binary not operand?
|
kholm
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
|
|
|
|
#146956 - 2005-09-06 10:44 AM
Re: what is this binary not operand?
|
Richard H.
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
|
|
|
|
#146959 - 2005-09-10 12:32 AM
Re: what is this binary not operand?
|
iffy
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
|
|
|
|
#146961 - 2005-09-10 03:28 PM
Re: what is this binary not operand?
|
iffy
Starting to like KiXtart
Registered: 2005-05-29
Posts: 149
Loc: The Netherlands
|
LOL... now you've made me feel old and extinct
|
Top
|
|
|
|
#146965 - 2005-09-12 09:59 AM
Re: what is this binary not operand?
|
Richard H.
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
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
|
|
|
|
Moderator: ShaneEP, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
1 registered
(mole)
and 405 anonymous users online.
|
|
|