Jump to content

input parameters / combination of values

Recommended Posts

I'm not good with bit-wise operations/math.  That said, what is the best way to write a routine that checks an input parameter for possible combinations of values ... similar to how, say, MSGBOX flags work.

So, if someone calls sampleRoutine(16+32+8), the value in sampleRoutine will be 56.  So, what is the best way to know which "bits" are up so that the combinations (16, 32, and 8) can be deduced?

Edited by jaja714
Link to post
Share on other sites

Here's a simple way of converting a number to its binary representation. After you have that, you can convert it to whatever you'd like.

Func DecodeBits($iNumber)
     Local $iOutBits = ""
          $iOutBits &= String(Int(Mod($iNumber, 2)))
          $iNumber = $iNumber / 2
     Until $iNumber < 1
     $iOutBits = StringReverse($iOutBits)
     Return StringFormat("%08i", $iOutBits)
EndFunc   ;==>DecodeBits
ConsoleWrite("Bits set: " & DecodeBits(56) & @CRLF)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

Thanks but I'm looking more for a way to apply it to input parameters, similar to MSGBOX flags.  How best can we decode a combination into its original components?

Here's a good example I put together.  Is there a better way to write this without hard-coding the bit positions (0, 1, 2, 3)?

Global $pgDrive = 1
Global $pgDir = 2
Global $pgName = 4
Global $pgExt = 8
Func _pathGet($file,$flags)
    Dim $szDrive, $szDir, $szName, $szExt,$szRet=''
    _PathSplit($file, $szDrive, $szDir, $szName, $szExt)
    If BitAnd(BitShift($flags,0),1) Then $szRet = $szRet & $szDrive
    If BitAnd(BitShift($flags,1),1) Then $szRet = $szRet & $szDir
    If BitAnd(BitShift($flags,2),1) Then $szRet = $szRet & $szName
    If BitAnd(BitShift($flags,3),1) Then $szRet = $szRet & $szExt
    Return $szRet
EndFunc   ;==>_pathGet

ConsoleWrite(_pathGet('c:\program files\46\text.c',$pgName+$pgExt)&@CRLF)
ConsoleWrite(_pathGet('c:\program files\46\text.c',$pgDrive+$pgDir)&@CRLF)


Edited by jaja714
Link to post
Share on other sites

Here a generic extractor  (as long as you create an array, in this example)

Global $pgDrive = 1
Global $pgDir = 2
Global $pgName = 4
Global $pgExt = 8
Local $szDrive, $szDir, $szName, $szExt
_PathSplit(@ScriptDir & '\Temp.au3', $szDrive, $szDir, $szName, $szExt)
Local $aPath [] = [$szDrive, $szDir, $szName, $szExt]

Func _extract (ByRef $aArray, $iFlags)
  Local $sReturn = ""
  For $b = 0 to UBound ($aArray)-1
    If BitAND (2^$b, $iFlags) Then $sReturn &= $aArray[$b]
  Return $sReturn


Link to post
Share on other sites

Actually, _PathSplit returns an array, so, the code above could be optimized.  However, instead of looping through an array, perhaps a more direct way would be to use the Log builtin function which is the inverse of exponentiation.  Its been a while since my last math class so I actually googled "opposite of exponents" to find this out.

Here is what I have now:

Global $PG_DRV = 1
Global $PG_DIR = 2
Global $PG_NAM = 4
Global $PG_EXT = 8
Func _pathGet($file, $flags = 15)
    Dim $szDrv, $szDir, $szNam, $szExt, $szRet=''
    _PathSplit($file, $szDrv, $szDir, $szNam, $szExt)
    If BitAND(BitShift($flags, Log($PG_DRV) / Log(2)), 1) Then $szRet = $szRet & $szDrv
    If BitAND(BitShift($flags, Log($PG_DIR) / Log(2)), 1) Then $szRet = $szRet & $szDir
    If BitAND(BitShift($flags, Log($PG_NAM) / Log(2)), 1) Then $szRet = $szRet & $szNam
    If BitAND(BitShift($flags, Log($PG_EXT) / Log(2)), 1) Then $szRet = $szRet & $szExt
    Return $szRet
EndFunc   ;==>_pathGet


Link to post
Share on other sites

Well, for 4 bits flag, I suppose it is alright but imagine that you need to extract 8 or 16 bits flag.  The approach I proposed is simpler and it is adaptable to any array size...

Link to post
Share on other sites

Right, Nine's approach is more flexible.
But jaja714, as you really seem focused on _PathGet(), here is a way without BitAnd & the gang :

#include <File.au3>
Global $pgDrive = 1, $pgDir = 2, $pgName = 3, $pgExt = 4

ConsoleWrite(_pathGet('c:\program files\46\text.c', $pgDrive, $pgDir) & @CRLF)
ConsoleWrite(_pathGet('c:\program files\46\text.c', $pgName, $pgExt)  & @CRLF)

Func _PathGet($file, $par2, $par3 = "", $par4 = "", $par5 = "")
    Local $szRet = '', $aPathSplit = _PathSplit($file, "", "", "", "")
    For $i = 2 To @NumParams
        $szRet &= $aPathSplit[Eval("par" & $i)]
    Return $szRet
EndFunc   ;==>_PathGet


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...