#158917 - 2006-03-14 02:07 PM
Re: Help converting VBS code to KiXtat
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Oh, how does one adequately check to see if a bit is toggled on in oredr to use simple math to turn a flag on or off? You use bitwise oparations. So why use bitwise operators just to then use addition and subtraction?
So to simplify:
Code:
; To check a flag a flag: If $objUserFlags & $ADS_UF_DONT_EXPIRE_PASSWD "Flag is turned ON" ? Else "Flag is turned OFF" ? EndIf
When using bitwise operations it really is necessary to check if a bit is toggled or not. You can just set it or turn it off without regard for its previous state unless your program flow is dependent on the bit value.
Code:
;To clear a flag $objUserFlags=$objUserFlags & ~$ADS_UF_DONT_EXPIRE_PASSWD
Code:
;to set a flag $objUserFlags=$objUserFlags | $ADS_UF_DONT_EXPIRE_PASSWD
|
Top
|
|
|
|
#158918 - 2006-03-14 05:10 PM
Re: Help converting VBS code to KiXtat
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Oh, how does one adequately check to see if a bit is toggled on in oredr to use simple math to turn a flag on or off? You use bitwise oparations. So why use bitwise operators just to then use addition and subtraction?
Errr yes If you read back you'll see that I don't at any point advocate using simple maths instead of bitwise operations, in fact I stress that it's a bad idea.
The simple maths examples are there to:
- To complete the story for those people who may not get complex bitwise operations but can readily understand bitwise AND and ORs as conditional tests.
- To give an alternative for anyone who is revision locked - bitwise NOT is only available with recent versions of KiXtart
- To demonstrate a KiXtart version of the technique of (re)setting bits which for some reason appears to be commonly used in VB scripts.
|
Top
|
|
|
|
#158920 - 2006-03-14 05:43 PM
Re: Help converting VBS code to KiXtat
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
This was not a dig. It was only to make a point for the novice readers.
|
Top
|
|
|
|
#158922 - 2006-03-15 10:24 AM
Re: Help converting VBS code to KiXtat
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
I had a bit of a eureka moment about 03:00 this morning, which leads me to be less adamant about using only bitwise operators to reset bits.
The problem is that scripting languages like KiXtart are "loosely typed". In particular, KiXtart uses sub-types of variant for all it's basic data types.
This is especially an issue when using constructs like the bitwise NOT "~". A bitwise NOT reverses the truth of each bit in the parameter. However, the size of the result is determined by the parameter.
The results of ~0 could be 255, 65535, 4294967295 or 18446744073709551615 depending on whether the "0" is interpreted as (using Windows terminology) an 8 bit byte, a 16 bit word a 32 bit dword or a 64 bit qword.
In KiXtart it is currently simple and everything will be treated as a dword. If you need a byte or word result you have to explicitly cap it, for example "&FF & ~0" for a byte and "&FFFF & ~0" for a word.
In other loosely typed languages small values might be interpreted as a byte or a word which could lead to truncation.
For example, if you wanted to reset flag #5 (value 32) you might do: Code:
$iFlags=3112 $iFlags & ~32 ?
In KiXtart this will work, and $iFlags will be set to 3080.
But what if you are using a loosely typed language which interprets the 32 as being a byte? You can simulate this in KiXtart: Code:
$iFlags=3112 $iFlags & ( ~32 & &FF) ?
The result is 8, which is clearly wrong.
The same problem will occur when small numbers are interpreted as being 16 bit integers.
If you know the size of the data that you are attempting to reset the bit in you can explicitly convert the value to the correct type using casts or functions, for example "$iFlags & ~CDbl(32)"
Using a conditional test and subtraction (or XOR) to reset the bit is data size independant and might be preferable where the size of the data cannot be pre-determined or guaranteed.
|
Top
|
|
|
|
#158923 - 2006-03-15 04:20 PM
Re: Help converting VBS code to KiXtat
|
Howard Bullock
KiX Supporter
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Very good post Richard. This is something I had not considered to date.
|
Top
|
|
|
|
#158930 - 2006-03-15 07:10 PM
Re: Help converting VBS code to KiXtat
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11623
Loc: CA
|
ROFL - You have too much insight to the US Law making strategy there Jooel.
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 248 anonymous users online.
|
|
|