Page 2 of 2 <12
Topic Options
#158917 - 2006-03-14 02:07 PM Re: Help converting VBS code to KiXtat
Howard Bullock Offline
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

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#158918 - 2006-03-14 05:10 PM Re: Help converting VBS code to KiXtat
Richard H. Administrator Offline
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:
  1. 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.
  2. To give an alternative for anyone who is revision locked - bitwise NOT is only available with recent versions of KiXtart
  3. 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
#158919 - 2006-03-14 05:27 PM Re: Help converting VBS code to KiXtat
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
d. To demonstrate how NOT to do it.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#158920 - 2006-03-14 05:43 PM Re: Help converting VBS code to KiXtat
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#158921 - 2006-03-14 06:56 PM Re: Help converting VBS code to KiXtat
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Well though not new, I have not studied or used that type of math in decades on my own, so I appreciate the input and feedback from both Howard and Richard.

Thanks for the lesson and information guys.

Top
#158922 - 2006-03-15 10:24 AM Re: Help converting VBS code to KiXtat
Richard H. Administrator Offline
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 Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#158924 - 2006-03-15 04:36 PM Re: Help converting VBS code to KiXtat
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
just for the records...

http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=157126&an=0&page=0#157126

Same question asked, solved, but not with that complete explanation
_________________________



Top
#158925 - 2006-03-15 04:39 PM Re: Help converting VBS code to KiXtat
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Richard,
Please consider reassembling your words of wisdom into a FAQ and post it in the FAQ forum so more can benefit.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#158926 - 2006-03-15 04:43 PM Re: Help converting VBS code to KiXtat
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
I'd like to second that... Anyone to third it ?
_________________________



Top
#158927 - 2006-03-15 06:29 PM Re: Help converting VBS code to KiXtat
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I can third it.
although, it's not must as I already learned it.

just that it would be beneficial for others too.
_________________________
!

download KiXnet

Top
#158928 - 2006-03-15 06:31 PM Re: Help converting VBS code to KiXtat
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Motion carried
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#158929 - 2006-03-15 06:50 PM Re: Help converting VBS code to KiXtat
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
it will face the US senate and will be turned down as it introduces threats to national security
_________________________
!

download KiXnet

Top
#158930 - 2006-03-15 07:10 PM Re: Help converting VBS code to KiXtat
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
ROFL - You have too much insight to the US Law making strategy there Jooel.
Top
#158931 - 2006-03-15 07:13 PM Re: Help converting VBS code to KiXtat
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, I watched the reality show called "banning the selling of or harbours to terrorists" and I liked it.
like les' sig says, stupidity is human right of every soul...
_________________________
!

download KiXnet

Top
Page 2 of 2 <12


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

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

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

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org