#80527 - 2001-12-05 01:47 PM
Bitwise unary "NOT" operator.
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
We need a bitwise unary "NOT" operator to complement the "|" and "&" operators.This will be used to construct more complex gates like XOR, and to reset binary bits. Example: ; Set bit 7 in $iFlag $iFlag=$iFlag | &0040 ; Reset bit 7 in &iFlag $iFlag=$iFlag & ~&0040 Where "~" is a bitwise NOT. EDIT Changed "boolean" to "bitwise" in the text and title to make things clearer [ 07 December 2001: Message edited by: Richard Howarth ]
|
|
Top
|
|
|
|
#80528 - 2001-12-05 03:22 PM
Re: Bitwise unary "NOT" operator.
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
lol - your doing the same thing i'm doing. Agreed, we need the one's complement not operator. Here's what I have so far:
break on if testbit(9,8) ; is bit 4 (from the right) of 1001 set ? (it is) ? setbit(9,4) ; set bit 3 on (answer should be 1101 or 13 endif
exit 1
function setbit($a,$b) $setbit = $a | $b endfunction
function testbit($a,$b) $testbit = $a & $b endfunction
function clearbit($a,$b) ; To be done endfunction
Like some c programmers (k&r man myself), I carry around a header file of usefull macro's. And especially I use these three all the time ... wouldn't be caught dead without them :
#define SETBIT(x, b) ((x) |= (b)) #define CLEARBIT(x, b) ((x) &= ~(b)) #define TESTBIT(x, b) ((x) & (b))
So just need to work-out the clearbit math and we're SET (sort-of-speak) : -Shawn [ 05 December 2001: Message edited by: Shawn ]
|
|
Top
|
|
|
|
#80533 - 2001-12-05 09:58 PM
Re: Bitwise unary "NOT" operator.
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Richard,I've suggested these before and I would imagine Ruud has seen/thought of these as well. But at the risk of turning KiXtart into a mish-mash of C and VB I'll throw these out again anyway. As a C programmer yourself, I know you already appreciate the "handyness" of autoincrement. And in a very small way, from a performance standpoint, are key targets for any kind of script engine optimizing 
$count++ ; automatically increment by 1 $count-- ; automatically decrement by 1$count += 10 ; same as $count = $count + 10 $count -= 10 ; same as $count = $count - 10
I can't count how many times i've koded this: $index = $index + 1 all the while grinding my teeth and tensing my shoulders and biting my tounge ... but - Ruud obviously has a master plan for KiXtart and he certainly hasn't steered us wrong yet. But I would love to get yours and the boards thoughts in regards to these ideas (borrowing/mixing syntax from other languages) or if KiXtart should stay true to it's BASIC roots ? -Shawn
|
|
Top
|
|
|
|
#80535 - 2001-12-07 04:49 AM
Re: Bitwise unary "NOT" operator.
|
kholm
Korg Regular
   
Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
|
Shawn,Another way of doing the same job: ClearBit() Or Not_or() I had hidden an other way of clearing bits, as function Not_Or() in UsrMustChgPwd() Function Not_Or($Num,$NotBin) ; Binary Not Or ; Clears the bits set in $Num, that are also set in $NotBin $Num = 0 + $Num $NotBin = 0 + $NotBin $Not_Or = ($Num | $NotBin) - $NotBin EndFunction My reason for calling the function Not_Or() was: You use binary Or to add a bit to a Number Not_Or() will do the opposite (Remove/clear a bit) You rely on -1 is transformed into largest possible numer, because negative numbers are not alowed in KiXtart, so i believe your way is just as safe as Richards: &FFFFFFFF instead of -1, as long as KiXtart dosn't allow negative numbers. I'm not shure if it would be a good idea to implement your C++ statements, or not: $count++ ; automatically increment by 1 $count-- ; automatically decrement by 1 $count += 10 ; same as $count = $count + 10 $count -= 10 ; same as $count = $count - 10 What about: ++$count --$count In for-next loops, do something, increace/decrease after Other KiX-users might want to implement the syntax of their favorite programming language. I would prefere the pascal syntax: Inc($Count) for increasing $count by one Inc($Count, $x) for increasing $count by $x Dec($Count) for decreasing $count by one Dec($Count, $x) for decreasing $count by $x I believe that the pascal-syntax would be easy to implement for Ruud. But i won't make it a first priority wish to get these functions included (I could make an UDF to do it)
What about performance: Of course it would be better for performance to have the funtions: Inc() and Dec() and operators (++, --, +=, -=) implemeted in KiXtart, than executing UDF's. I can make my pascal UDf's, but you canīt make C-UDF function ++(), it would be illegal. -Erik
ps. I think that Ruud has made a great effort to make the syntax for string- and object handling very similar to the visual basic syntax, so if the the KiX syntax gets to be like VB, at least we don't have to 'rewind' when going from VB or Java to KiXtart!! 
cj- You can't use ~ it is opted by Richard as boolean not, to keep this VB like ! is the not-operator. Is ~ a boolean not in VB ?
|
|
Top
|
|
|
|
#80536 - 2001-12-07 10:58 AM
Re: Bitwise unary "NOT" operator.
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
CJ, Both "!" and "~" are operators in 'C', but they have different meanings, "!" is a unary logical NOT, ie it is used to reverse the value of an expression from FALSE (zero)to TRUE (not-zero) and vice-versa. It will not preserve the value of the expression for example "!2" is zero as is "!4". The "~" operator on the other hand is a bit-wise NOT, specifically it is a ones-complement NOT. This reverses the state of each of the bits in the value from "1" to "0" and vice-versa. This preserves the information. I don't know VB, so I don't know how it supports the different modes, or what the "!" operator implies.On the subject of "borrowing" elements from other languages I'd say, it depends. Adding the pre- and post-fix operators like "++" because they execute faster is a great idea, but is no good if the downside is that KiXtart is made more complicated and the interpreter is slowed down for the rest of the script. The rumored facility for tokenisation or byte-code precompiling will remove a lot of that overhead of course. Sometimes you have to sit back and say "does this really fit in" with KiXtarts purpose? For example I'd love to see associative arrays taken from AWK or PERL. These are arrays which are keyed by a variant subscript rather than a numeric subscript, but this is a very complex feature and can be achieved in other ways. One thing we really need are static variables for UDFs. Until we have these UDFs will have to expose global variables to maintain state information. [ 07 December 2001: Message edited by: Richard Howarth ]
|
|
Top
|
|
|
|
#80537 - 2001-12-07 02:12 PM
Re: Bitwise unary "NOT" operator.
|
Alex.H
Seasoned Scripter
Registered: 2001-04-10
Posts: 406
Loc: France
|
Richard, CJ VB don't support these operators. It use it as NOT, OR, XOR, .. They always are explicit as words, and not as single char operators. Since i lately read lots of C example, even if i didn't know C language, i can identify such operators, but think that other kix users may not know what C is, and that was the most valuable thing to kixtart : a really easy language to read/learn. As i personnaly don't care, i don't think it will be good to add such operators, or only if they exist as C AND VB flavors (or something else easy to read than cabalistics symbols). Hope i'm understable. For the static variables, dunno why but i had in mind that Kix 4.01 support it. Seems it was a dream, but yes, we really need it.Shawn, You're getting dirty here. Wanting to beat Quake 3 or UT with Kixtart ?  [ 07 December 2001: Message edited by: Popovk ]
_________________________
? getobject(Kixtart.org.Signature)
|
|
Top
|
|
|
|
#80538 - 2001-12-07 02:42 PM
Re: Bitwise unary "NOT" operator.
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Cabalistic symbols! ROFL That I like."Welcome to the KiXtart Coding Coven, wipe your feet and leave your sacrificial dagger by the door." The problem is that KiXtart 4.00 already has a "NOT", which is a logical operator, i.e. it works like C "!". As KiXtart supports "|" for bitwise OR, and "&" for bitwise AND which also match the C convention it seemed sensible to continue that way with the bitwise NOT. How does VB handle logical NOTs? You can't tell from the context if you want a logical or bitwise operation. Here is a very simple "C" program that demonstrates the difference:
code:
#include main(){ unsigned char c=170; printf("%d Logical NOT = %d, converted back=%d\n",c,!c,!(!c)); printf("%d Bitwise NOT = %d, converted back=%d\n",c,~c,~(~c)); }
Which will give the output:
code:
170 Logical NOT = 0, converted back=1 170 Bitwise NOT = -171, converted back=170
Now, if the code read "NOT c", how would you know which NOT was required?
|
|
Top
|
|
|
|
#80539 - 2001-12-07 03:07 PM
Re: Bitwise unary "NOT" operator.
|
Alex.H
Seasoned Scripter
Registered: 2001-04-10
Posts: 406
Loc: France
|
About this, it's the hard part of VB In fact, it use the type of the container variable to switch between logical and bitwise:
code:
Sub test() Dim btest As Boolean Dim ntest As Long btest = Not 170 ntest = Not 170 MsgBox "btest : " & btest & Chr(10) & "ntest : " & ntest End Sub
You'll get this :
code:
btest : True ntest : -171
VB don't know screen output, that's why the msgbox is here (well, until you add a complete module of API command to support dos box output)
_________________________
? getobject(Kixtart.org.Signature)
|
|
Top
|
|
|
|
#80541 - 2001-12-07 06:35 PM
Re: Bitwise unary "NOT" operator.
|
Alex.H
Seasoned Scripter
Registered: 2001-04-10
Posts: 406
Loc: France
|
No. In fact, False have the value 0, True have -1 But, when used for boolean calcul, True is always different than 0 Seems i've done a mistake here, as it working in this way : btest = not 170 => btest =-171 as -171 <> 0 so we got -171=true so btest= TrueTo really have the 'NOT 170' working as logical, it should be of type boolean. After rereading doc about it, VB switch to logical when one of the values is of boolean type. As it's not the case in our example, VB use OR as bitwise. Usually, i always use the Cbool() function when doing logical operations For your purpose, it should be like this :
code:
btest=not cbool(170)
and we get this result : btest=false
_________________________
? getobject(Kixtart.org.Signature)
|
|
Top
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
0 registered
and 633 anonymous users online.
|
|
|