# Binary Switching

## Recommended Posts

Alright well I have searched up and down these forums looking for anything to point me in the right direction and can not find anything....

How would I go about creating a function that would take a number and break it down....

for instance

1 option1

2 option2

4 option3

8 option4

16 option5

32 option6

64 option7

Say you wanted to run your script with options 1, 3, and 7

so you would just add together there numbers and use those numbers as the switch for example

Run("Example.exe" & " /69")

Your script than would run options 1, 3 ,7

I have played with the idea and I searched all around the forums for any udfs or other topics that this may have been covered.

##### Share on other sites

StringToBinary and then stringsplit

then an if test for:

if \$string[1] = 1 Then ....

completely oversimplified and probs not the most efficient way either, but do you see what I'm trying to do?

MDiesel

Edited by mdiesel

##### Share on other sites

\$example = 43 just an example number

If \$example > 64 then

\$64 = true

\$example -= 64

else

\$64 = false

endif

of course 43 is less than 64 so this first gets passed by

If \$example > 32 then

\$32 = True

\$example-=32

else

\$32 = False

endif

43 is greater than 32 so you can set option6 to wait for \$32 to equal true

43 - 32 = 11 so next to turn on will be \$8 making example = 3 turning on \$2 and finally \$1

Giggity

##### Share on other sites

StringToBinary and then stringsplit

then an if test for:

if \$string[1] = 1 Then ....

completely oversimplified and probs not the most efficient way either, but do you see what I'm trying to do?

MDiesel

the buildin StringToBinary ?

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

See the thing is.... What i wrote was just an example... There are 108 options total so to write a function to start at 2^108 and down one by one is fairly well just doesnt work

Edited by Sublime

##### Share on other sites

You guys are way off.

```#cs
1 option1
2 option2
4 option3
8 option4
16 option5
32 option6
64 option7
#ce

\$code = 1+4+16+64 ;85

If BitAND(\$code,1) Then ConsoleWrite("Option 1" & @CRLF)
If BitAND(\$code,2) Then ConsoleWrite("Option 2" & @CRLF)
If BitAND(\$code,4) Then ConsoleWrite("Option 3" & @CRLF)
If BitAND(\$code,8) Then ConsoleWrite("Option 4" & @CRLF)
If BitAND(\$code,16) Then ConsoleWrite("Option 5" & @CRLF)
If BitAND(\$code,32) Then ConsoleWrite("Option 6" & @CRLF)
If BitAND(\$code,64) Then ConsoleWrite("Option 7" & @CRLF)```

##### Share on other sites

Off? no. More than one way to skin a cat. If the result is a naked cat then I wouldn't say off. Your method is a better and probably preferred method. I myself am not familiar enough with Bitand to have known that. Now I know, and knowing is half the battle.

How very Wolverine of you, bub.

Giggity

##### Share on other sites

Thanks WeaponX

That should work perfectly

Much appreciated.

##### Share on other sites

the buildin StringToBinary ?

?? Is this at me or something else?

##### Share on other sites

?? Is this at me or something else?

That isn't a very clear question to me.

I do believe I did quote your post and was asking about the Funcs you mentioned. just wondering how you would do that with StringToBinary()

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Here is an example more in line with your goals. Here I use a zero based options list for a little trick with BitShift.

Code:

```#cs
1 option0
2 option1
4 option2
8 option3
16 option4
32 option5
64 option6
#ce

\$code = 1+4+16+64 ;85

For \$X = 0 to 6
If BitAND(\$code,BitShift(1,-\$X)) Then RunOptions(\$X)
Next

Func RunOptions(\$V)
Switch \$V
Case 0
ConsoleWrite("Option 0 - Running" & @CRLF)
Case 1
ConsoleWrite("Option 1 - Running" & @CRLF)
Case 2
ConsoleWrite("Option 2 - Running" & @CRLF)
Case 3
ConsoleWrite("Option 3 - Running" & @CRLF)
Case 4
ConsoleWrite("Option 4 - Running" & @CRLF)
Case 5
ConsoleWrite("Option 5 - Running" & @CRLF)
Case 6
ConsoleWrite("Option 6 - Running" & @CRLF)
EndSwitch
EndFunc```

Output:

```Option 0 - Running
Option 2 - Running
Option 4 - Running
Option 6 - Running```

##### Share on other sites

That isn't a very clear question to me.

I do believe I did quote your post and was asking about the Funcs you mentioned. just wondering how you would do that with StringToBinary()

sorry, I think I'm mssing something, Binary is not my strong point but - isn't it composed of 0's and 1's - so 3 = 11 ?

just testing it now I'm getting 0xnumber - so my post could not have worked.

or is this some form of binary unrelated/not the same as the base 2 numeral system I'm thinking of?

##### Share on other sites

@MDiesel

It is binary why no matter how you add up the numbers when you break it down you get the correct options

Look:

128 64 32 16 8 4 2 1 <---- The numbers were using for adding

0 0 0 0 0 0 0 0 <---- Just now if you picked the number above it just like putting a 1 below that number

1 option1

2 option2

4 option3

8 option4

16 option5

32 option6

64 option7

Anotherwords 69 is 01100011 in binary another words as per my example earlier options 7, 6, 2, and 1 would run

On a side note

This is kind of what I am going to be using. I am not on a computer with Scite or even autoit so cant test it.

```Dim \$oArray, \$count = 0

Local \$code = 85
Local \$Remainder = \$code

For \$y = 6 to 0 Step -1
Local \$x = 2^\$y
If BitAND(\$Remainder,\$x) Then \$oArray[\$count]
Local \$Remainder = \$code - \$x
\$count += 1
Next

End```

//EDIT I didnt see your repost WeaponX. Would BitSHIFT work for shifting by exponents?

Edited by Sublime

##### Share on other sites

just testing it now I'm getting 0xnumber - so my post could not have worked.

That was the only point I was trying to make.

"Stop assuming" is a useful statements in this case.

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

The trick I did with BitShift basically converts an integer into its bit position. So I do a loop 0-6 and BitShift takes that number and converts it to 1,2,4,8,16,32,64.

##### Share on other sites

O_o oh so that how it works....

Not familiar with the Bit functions what so ever.. But that is alot more effecient and I dont have to use the extra variable... Thanks the last snipplet you put out should work perfectly

##### Share on other sites

And if you like Enums, there is another possibility (you don't have to care abot the exact values)

```Global Enum Step *2 \$OPT_1, \$OPT_2, \$OPT_3, \$OPT_4, \$OPT_5

\$OPTIONS = BitOR(\$OPT_3,\$OPT_5, \$OPT_1)

If _IsBit(\$OPTIONS, \$OPT_1) Then MsgBox(0, '', "OPT_1")
If _IsBit(\$OPTIONS, \$OPT_2) Then MsgBox(0, '', "OPT_2")
If _IsBit(\$OPTIONS, \$OPT_3) Then MsgBox(0, '', "OPT_3")
If _IsBit(\$OPTIONS, \$OPT_4) Then MsgBox(0, '', "OPT_4")

Func _IsBit(\$value, \$test)
Return BitAND(\$value,\$test)=\$test
EndFunc```
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

##### Share on other sites

108 bit flags? 2^108?

Your problems go farther than how to split one byte up into bits.

I'm thinking you're not going to be using addition to build a single all-inclusive 'numeric' parameter such as 324518554123456781234567812345678.

You could possibly use additon for each group of 8 flags, generating a number between 0-255, then string together 14 of those (which would total 112 flags, room for expansion!) then split them back out and process them a byte at a time. Something like this:

```; calling program
Dim \$Group[15]
\$Group[1] = 128 + 32 + 8
\$Group[2] = 64 + 32 + 8 + 1
\$Group[3] = 128 + 64 + 32 + 4 + 2; etc, etc

For \$x = 1 to 14
\$Parameter &= Chr(\$Group[\$x])
Next

ShellExecute("C:\MyProgram.exe", \$Parameter)

;----------------------------------------------------------------

; receiving program
\$Group = StringSplit(\$CmdLine[1])
For \$x = 1 to 14
Tooltip(Asc(\$Group[\$x]));break out bits from each 8-bit group using one of the prior examples
Next```

Edit: group/groups typo

Edited by Spiff59

## Create an account

Register a new account

×

• Wiki

• Back

• Git