I'm not a programmer, I'm just a novice AutoHotKey user; so there may
be more efficient ways to do the coding for this Demo. I wanted to use
the Bit Values of a variable as flags for some script operations today, but
was unfamiliar with that technique. After doing some research on the
subject, I wrote this Demo to help myself learn the coding. Since I was
unable to find very much info in the forum on the subject, I thought I
would share my research and Demo with everyone. The next person who
goes searching for a sample script on the use of Bit Flags may find this
Demo helpful.
Code:
/*
Demo illustrating the use of Bit Flags
with a special thanks to Laszlo for sharing
his short and sweet little technique for the
retrieval of Bit Flags (which I've used here).
Variables used:
x = The variable we're going to set and reset the bits in
s = The variable containing the Bit to be Set or Reset
msg = Bit Status Report
var = temporary variable used by the set and reset functions
bit = temporary variable used by the set and reset functions
ds = Gui 1 Variable
dr = Gui 2 Variable
Hotkeys:
F8 = Set a Bit and Display the Bit Table
F9 = Reset a Bit and Display the Bit Table
F10 = Display the Bit Table
*/
x = 0
F8::
s = 0
s = Exit
Gui,1:Destroy
List1= Exit||0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15
Gui, 1: Font, Arial S11
Gui, 1: Add, Text, x9 y7 w112 h30 , Select Bit to Set
Gui, 1: Add, DropDownList, x32 y35 w50 h310 vds gF8D, %List1%
Gui, 1: Show, center h360 w112, Select
sendinput, !{up}
return
F8D:
Gui, 1: Submit
Gui,1:Destroy
s = %ds%
If s != Exit
goto, F8C
Return
1GuiEscape:
1GuiClose:
exitapp
F8C:
if (s = "Exit")
return
x := set(x,s) ; Set the Bit
msg =
Loop, 16 ; Generate the Status Report
{
If A_Index < 11
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
else
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
}
msg := "Bit Status Weighted Value`n`n" . msg
MsgBox, 0, Current Bit Status,%msg%`nx = %x%
return
F9::
s = 0
s = Exit
Gui,2:Destroy
List1= Exit||0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15
Gui, 2: Font, Arial S11
Gui, 2: Add, Text, x6 y7 w112 h30 , Select Bit to Reset
Gui, 2: Add, DropDownList, x37 y35 w50 h310 vdr gF9D, %List1%
Gui, 2: Show, center h365 w122, Select
sendinput, !{up}
return
F9D:
Gui, 2: Submit
Gui,2:Destroy
s = %dr%
If s != Exit
goto, F9C
Return
2GuiEscape:
2GuiClose:
exitapp
F9C:
if (s = "Exit")
return
x := reset(x,s) ; Reset the Bit
msg =
Loop, 16 ; Generate the Status Report
{
If A_Index < 11
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
else
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
}
msg := "Bit Status Weighted Value`n`n" . msg
MsgBox, 0, Current Bit Status,%msg%`nx = %x%
return
F10:: ; Display Bit Status
msg =
Loop, 16 ; Generate the Status Report
{
If A_Index < 11
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
else
msg .= "Bit " . A_Index -1 . " : " . x>>(A_Index-1) & 1 . " ( " . 2**(A_Index - 1) . " )`n"
}
msg := "Bit Status Weighted Value`n`n" . msg
MsgBox, 0, Current Bit Status,%msg%`nx = %x%
return
; Set a Bit (if it's not set) by adding the appropriate power of 2 to the variable
set(var,bit) {
return (var += ((var>>bit) & 1) = 0 ? 2**bit : 0)
}
; Reset a Bit (if it's set) by subtracting the appropriate power of 2 from the variable
reset(var,bit) {
return (var -= ((var>>bit) & 1) = 1 ? 2**bit : 0)
}