AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Bit Flag Demo (for those interested in using this technique)

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Larry



Joined: 03 Mar 2005
Posts: 61

PostPosted: Sun Mar 07, 2010 8:18 am    Post subject: Bit Flag Demo (for those interested in using this technique) Reply with quote

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)
}
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Sun Mar 07, 2010 2:59 pm    Post subject: Reply with quote

a lil set of bin Ops with a code fo how to use 'em

Code:
; fun with bits
; (w) by derRaphael 2010-03-07

d:=0, bits := 8

loop, % bits
{
   setFlag( d, a_index )
   msgbox,0,% "flagged Index: " a_index,  % d2b(d)
}   

loop, % bits
{
   delFlag( d, a_index )
   msgbox,0,% "removed flag on index: " a_index,  % d2b(d)
}   

loop, % bits
{
   if ! ( A_Index & 1 )
      setFlag( d, A_Index )
}

MsgBox,0, All even Flags Set, % d2b(d)

; toggle flags one by one
Loop, % bits
{
   tglFlag( d, a_index )
}

MsgBox,0, All Flags Inverted, % d2b(d)

; this toggles all flags instantly, but d becomes an inverted 32bit value
; which it has been before, too ;)
d := ~d
msgbox,0,% "literal value of d: " d, % "Binary representation: " d2b(d,32)

; functions

; sets a flag at a specified index
setFlag( byref d, flagIdx )
{
   d |= ( 2**(flagIdx-1) )
}

; deletes a flag at a specified index
delFlag( byref d, flagIdx )
{
   if ( isFlag( d, flagIdx ) )
      tglFlag( d, flagIdx )
}

; toggles a flag at a specified index
tglFlag( byref d, flagIdx )
{
   d ^= ( 2**(flagIdx-1) )
}

; returns true if flag is set at a specified index
isFlag( byRef d, flagIdx )
{
   return d && ( 2**(flagIdx-1) )
}

; converts a decimal to a binary representation showing all bits
d2b( dec, bits=8 )
{
   loop,% bits
   {
      out .= ( dec >> (bits-a_index) ) & 1
   }
   return out
}


probly this helps getting a deeper understanding of the topic

greets
dR
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
derRaphael



Joined: 23 Nov 2007
Posts: 841
Location: ~/.

PostPosted: Sun Mar 07, 2010 3:12 pm    Post subject: Reply with quote

fun with bits II, this time swap two integer values

Code:

a := 123456
b := 654321

msgbox % a "/" b
swap(a,b)
msgbox % a "/" b

; short and sweet, interesting cuz it avoids the need for temp vars
swap(byref a, byref b)
{
   a^=b, b^=a, a^=b
}


greets
dR
_________________

    All scripts, unless otherwise noted, are hereby released under CC-BY
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Mar 07, 2010 5:02 pm    Post subject: Reply with quote

DerRaphael wrote:
swap two integer values...
It is an old trick, with a serious limitation: Swap(a,a) sets the variable "a" to 0, although it should keep "a" unchanged.

Temp01 has posted a better script:
Code:
Swap(ByRef Left, ByRef Right) {
    Left := (Right "", Right := Left) ; value of list is leftmost expr, terms are executed from left to right
}


Last edited by Laszlo on Sun Mar 07, 2010 5:44 pm; edited 2 times in total
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Mar 07, 2010 5:36 pm    Post subject: Reply with quote

Newer Windows versions have dll functions for bit maps, as discussed here, for example. These are pretty fast and handle arbitrary size maps.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group