Jump to content

Bitwise operations visualiser


Siao
 Share

Recommended Posts

Quick little gadget, not very flashy but does the job.

Could be helpful to those who don't quite understand how BitAND/OR/etc work, or just for some experimenting/testing.

Posted Image

code follows

#cs                                     #
    AutoIt bit operations visualiser v1
    by Siao
#ce                                     #

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>

Opt("GUIOnEventMode", 1)
;~ Opt("MustDeclareVars", 1)

Global $hGui, $cInput, $cResult, $cCalc, $cArrowDown, $cArrowUp, $cBtnDo, $cLbl
Global $aArrowDown[16] = [1,15,16,30,31,15,25,15,25,1,8,1,8,15,1,15]
Global $aArrowUp[16] = [1,15,16,0,31,15,25,15,25,30,8,30,8,15,1,15]

$hGui = GUICreate("AutoIt BitOps Visualizer", 400, 250, -1, -1, -1, -1)
GUISetOnEvent(-3, "SysEvents")
$cInput = GUICtrlCreateInput("", 20, 40, 186, 32)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
GUICtrlSetData(-1, 'BitAnd(3, 2)')
$cResult = GUICtrlCreateInput("", 280, 40, 100, 32)
GUICtrlSetFont(-1, 14, 400, 0, "MS Sans Serif")
$cCalc = GUICtrlCreateEdit("", 10, 108, 380, 120, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN))
GUICtrlSetFont(-1, 10, 800, 0, "Courier New")
$cArrowDown = GUICtrlCreateGraphic(60, 74, 32, 32)
_GraphicDrawLines($cArrowDown, $aArrowDown, 2, 0xbbaabb)
$cArrowUp = GUICtrlCreateGraphic(308, 75, 32, 32)
_GraphicDrawLines($cArrowUp, $aArrowUp, 2, 0xbbaabb)
$cBtnDo = GUICtrlCreateButton("=", 220, 40, 50, 30)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "_BitWisor")
$cLbl = GUICtrlCreateLabel('Enter expression and press "=" to calculate.', 20, 10, 250, 20)

GUISetState()
While 1
    Sleep(1000)
WEnd

Func SysEvents()
    Switch @GUI_CtrlId
        Case -3
            Exit
    EndSwitch
EndFunc
Func _BitWisor()
    Local $sRez = "", $sError = "", $sInput = GUICtrlRead($cInput), $aInput, $sOp, $sCalc, $i=0, $aTmp, $sTmp
    $aInput = StringRegExp($sInput, '\A[[:blank:]]*([[:alpha:]]+)[[:blank:]]*\((.+)\)', 1)
    If @error Then
        $sError = 'Invalid expression, foo!'
    Else
        $sInput = $aInput[0] & '(' & $aInput[1] & ')'
        GUICtrlSetData($cInput, $sInput)
        $sOp = StringUpper(StringTrimLeft($aInput[0], 3))
        $sRez = Execute($sInput)
        If @error Then
            $sError = 'Invalid expression, foo!'
        Else
            Switch $aInput[0]
                Case "bitand", "bitor", "bitxor"
                    $aTmp = _SplitParams($aInput[1])
                    For $i = 1 To $aTmp[0]
                        $sTmp &= _HexToBinBase(Execute($aTmp[$i])) & '   0x' & Hex(Execute($aTmp[$i])) & @CRLF & Chr(1) & @CRLF
                        $sCalc = StringReplace(StringTrimRight($sTmp, 3), Chr(1), $sOp) & _
                                "=" & @CRLF & _
                                _HexToBinBase($sRez) & '   0x' & Hex($sRez) & @CRLF                         
                    Next        
                Case "bitnot"
                    $sCalc =    _HexToBinBase(Execute($aInput[1])) & '   0x' & Hex(Execute($aInput[1])) & @CRLF & _
                                $sOp & @CRLF & _
                                "=" & @CRLF & _
                                _HexToBinBase($sRez) & '   0x' & Hex($sRez) & @CRLF 
                Case "bitshift"
                    $aTmp = _SplitParams($aInput[1])
                    If Execute($aTmp[2]) > 0 Then
                        $sTmp = $sOp & " right by " & Execute($aTmp[2])
                    ElseIf Execute($aTmp[2]) < 0 Then
                        $sTmp = $sOp & " left by " & Abs(Execute($aTmp[2]))
                    Else
                        $sTmp = $sOp & " by 0"
                    EndIf
                    $sCalc =    _HexToBinBase(Execute($aTmp[1])) & '   0x' & Hex(Execute($aTmp[1])) & @CRLF & _
                                $sTmp & @CRLF & _
                                "=" & @CRLF & _
                                _HexToBinBase($sRez) & '   0x' & Hex($sRez) & @CRLF                 
                Case "bitrotate"
                    $sError = "BitRotate currently unsupported."
                Case Else
                    $sError = "Not a bitwise operation."
            EndSwitch
        EndIf
    EndIf
    GUICtrlSetData($cResult, $sRez)
    If $sError <> "" Then
        GUICtrlSetData($cCalc, $sError)
    Else
        GUICtrlSetData($cCalc, $sCalc)
    EndIf
EndFunc
#cs _HexToBinBase()
    Converts a number to binary base string
#ce
Func _HexToBinBase($hex)
    Local $b = ""
    For $i = 1 To 32
        $b = BitAND($hex, 1) & $b
        $hex = BitShift($hex, 1)
    Next
    Return $b
EndFunc
#cs _SplitParams()
    Crappy AutoIt param string parser
    Should choke on quoted commas
    Returns StringSplit-like array
#ce
Func _SplitParams($sParams)
    Local $aParams, $i, $br1, $br2, $aRet[1]
    $aParams = StringSplit($sParams, ",")
    For $i = $aParams[0] To 1 Step -1
        StringReplace($aParams[$i], ")", "")
        $br1 = @extended
        StringReplace($aParams[$i], "(", "")
        $br2 = @extended
        If $br1 <> $br2 Then
            $aParams[$i-1] &= "," & $aParams[$i]
            $aParams[$i] = ""
        EndIf
    Next
    For $i = 1 To $aParams[0]
        If $aParams[$i] <> "" Then 
            Redim $aRet[UBound($aRet)+1]
            $aRet[UBound($aRet)-1] = $aParams[$i]
        EndIf
    Next
    $aRet[0] = UBound($aRet)-1
    Return $aRet
EndFunc
#cs _GraphicDrawLines()
    Draws lines, duh
    Params: $cID - control ID from GuiCtrlCreateGraphic
            $aCoords - 1D array containing coordinate pairs ( $a[0]=x0, $a[1]=y0, $a[2]=x1, $a[3]=y1, etc.)
            $iPenSize, $iColor, $iColorBk - line options
#ce
Func _GraphicDrawLines($cID, $aCoords, $iPenSize = 2, $iColor = 0, $iColorBk = -1)
    GUICtrlSetGraphic($cID, $GUI_GR_PENSIZE, $iPenSize)
    GUICtrlSetGraphic($cID, $GUI_GR_COLOR, $iColor, $iColorBk)
    GUICtrlSetGraphic($cID, $GUI_GR_MOVE, $aCoords[0], $aCoords[1])
    For $i = 2 To UBound($aCoords)-1 Step 2
        GUICtrlSetGraphic($cID, $GUI_GR_LINE, $aCoords[$i], $aCoords[$i+1])
    Next
EndFunc

or

bitops_visual.au3

Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

Downright handy little utility. I just used it to convert

BitOR( 0x0003, 0x0040, 0x00200000)

to

0x00200043

and the control worked the same as when I used the BitOR()

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Just an idea but just as in PHP, leaving a user input Execute() wide open is not secure.

I would allow the user to enter only a comma seperated list of digits, then have radio buttons below for the type of operation to perform. This way it will only perform Bit* functions instead of Execute().

Also, I like the usage of the native Graphic functions to create the arrows.

Edited by weaponx
Link to comment
Share on other sites

Nice!

Very good for demonstrations.

Good job!

Só o que posso lhe dizer, bom é quando faz mal!My work:Au3Irrlicht - Irrlicht for AutoItMsAgentLib - An UDF for MSAgentAu3GlPlugin T2 - A 3D plugin for AutoIt...OpenGl Plugin - The old version of Au3GlPlugin.MAC Address Changer - Changes the MAC AddressItCopter - A dragonfly R/C helicopter simulator

VW Bug user

Pinheiral (Pinewood) city:

http://pt.wikipedia.org/wiki/Pinheiral

Link to comment
Share on other sites

I would allow the user to enter only a comma seperated list of digits, then have radio buttons below for the type of operation to perform. This way it will only perform Bit* functions instead of Execute().

I like being able to paste in an existing Bit* statement and having it calculated.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 1 year later...

BitRotate added to this great utility from Siao.

;
#cs
    AutoIt bit operations visualiser v1
    by Siao
    [url="http://www.autoitscript.com/forum/index.php?s=&showtopic=62304&view=findpost&p=466668"]http://www.autoitscript.com/forum/index....=&showtopic=62304&view=findpost&p=466668[/url]
    ==================================================
    Existing alpha color ARGB  with new color: (ARGB - RGB) + newRGB = ARGB
    bitor(BitAND(0x98765432, 0xFF000000),BitAND(0x123457, 0x00FFFFFF))
    10011000000000000000000000000000   0x98000000OR
    00000000000100100011010001010111   0x00123457
    =
    10011000000100100011010001010111   0x98123457
    ==================================================
    add Alpha (255)   to color RGB (0x123457):   A + RGB = ARGB
    bitor(bitshift(255,-24),BitAND(0x123457, 0x00FFFFFF))
    11111111000000000000000000000000   0xFF000000
    OR
    00000000000100100011010001010111   0x00123457
    =
    11111111000100100011010001010111   0xFF123457
    =================================================
    ; ARGB2ABGR & Visa Versa
    ; ConsoleWrite("0x" & Hex(BitOR(BitShift(BitAND(0x12345678, 0x00ff0000), 16), BitAND(0x12345678, 0xff00ff00), BitShift(BitAND(0x12345678, 0x000000ff), -16))))
    =================================================
    RGB2BGR & Visa Versa
    ConsoleWrite("0x" & Hex(BitOR(BitShift(BitAND(0x345678, 0x0ff0000), 16), BitAND(0x345678, 0x00ff00), BitShift(BitAND(0x345678, 0x0000ff), -16))))
    =================================================
#ce

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)
;~ Opt("MustDeclareVars", 1)

Global $hGui, $cInput, $cResult, $cCalc, $cArrowDown, $cArrowUp, $cBtnDo, $cLbl
Global $aArrowDown[16] = [1, 15, 16, 30, 31, 15, 25, 15, 25, 1, 8, 1, 8, 15, 1, 15]
Global $aArrowUp[16] = [1, 15, 16, 0, 31, 15, 25, 15, 25, 30, 8, 30, 8, 15, 1, 15]

$hGui = GUICreate("AutoIt BitOps Visualizer", 800, 250, -1, -1, $WS_SIZEBOX, -1)
GUISetOnEvent(-3, "SysEvents")
$cInput = GUICtrlCreateInput("", 20, 40, 586, 32)
GUICtrlSetFont(-1, 12, 600, 0, "MS Sans Serif")
GUICtrlSetData(-1, 'BitAnd(3, 2)')
$cResult = GUICtrlCreateInput("", 680, 40, 100, 32)
GUICtrlSetFont(-1, 12, 600, 0, "MS Sans Serif")
$cCalc = GUICtrlCreateEdit("", 10, 108, 780, 120, BitOR($ES_AUTOVSCROLL, $WS_VSCROLL, $ES_AUTOHSCROLL, $WS_HSCROLL, $ES_WANTRETURN))
GUICtrlSetFont(-1, 10, 800, 0, "Courier New")
$cArrowDown = GUICtrlCreateGraphic(60, 74, 32, 32)
_GraphicDrawLines($cArrowDown, $aArrowDown, 2, 0xbbaabb)
$cArrowUp = GUICtrlCreateGraphic(708, 75, 32, 32)
_GraphicDrawLines($cArrowUp, $aArrowUp, 2, 0xbbaabb)
$cBtnDo = GUICtrlCreateButton("=", 620, 40, 50, 30)
GUICtrlSetFont(-1, 18, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "_BitWisor")
$cLbl = GUICtrlCreateLabel('Enter expression and press "=" to calculate.', 250, 5, 250, 35)
GUICtrlSetFont(-1, 10, 800, 0, "Courier New")

GUISetState()
While 1
    Sleep(1000)
WEnd

Func SysEvents()
    Switch @GUI_CtrlId
        Case -3
            Exit
    EndSwitch
EndFunc   ;==>SysEvents

Func _BitWisor()
    Local $sRez = "", $sError = "", $sInput = GUICtrlRead($cInput), $aInput, $sOp, $sCalc, $i = 0, $aTmp, $sTmp, $sSize
    $aInput = StringRegExp($sInput, 'A[[:blank:]]*([[:alpha:]]+)[[:blank:]]*((.+))', 1)
    ;ConsoleWrite("$aInput[1] " & $aInput[1] & @CRLF)
    If @error Then
        $sError = 'Invalid expression, foo!'
    Else
        $sInput = $aInput[0] & '(' & $aInput[1] & ')'
        GUICtrlSetData($cInput, $sInput)
        $sOp = StringUpper(StringTrimLeft($aInput[0], 3))
        $sRez = Execute($sInput)
        If @error Then
            $sError = 'Invalid expression, foo!'
        Else
            Switch $aInput[0]
                Case "bitand", "bitor", "bitxor"
                    $aTmp = _SplitParams($aInput[1])
                    For $i = 1 To $aTmp[0]
                        $sTmp &= _HexToBinBase(Execute($aTmp[$i])) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex(Execute($aTmp[$i])) & @CRLF & Chr(1) & @CRLF
                        $sCalc = StringReplace(StringTrimRight($sTmp, 3), Chr(1), $sOp) & _
                                "=" & @CRLF & _
                                _HexToBinBase($sRez) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex($sRez) & @CRLF
                    Next
                Case "bitnot"
                    $sCalc = _HexToBinBase(Execute($aInput[1])) & '   0x' & Hex(Execute($aInput[1])) & @CRLF & _
                            $sOp & @CRLF & _
                            "=" & @CRLF & _
                            _HexToBinBase($sRez) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex($sRez) & @CRLF
                Case "bitshift"
                    $aTmp = _SplitParams($aInput[1])
                    If Execute($aTmp[2]) > 0 Then
                        $sTmp = $sOp & " right by " & Execute($aTmp[2])
                    ElseIf Execute($aTmp[2]) < 0 Then
                        $sTmp = $sOp & " left by " & Abs(Execute($aTmp[2]))
                    Else
                        $sTmp = $sOp & " by 0"
                    EndIf
                    $sCalc = _HexToBinBase(Execute($aTmp[1])) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex(Execute($aTmp[1])) & @CRLF & _
                            $sTmp & @CRLF & _
                            "=" & @CRLF & _
                            _HexToBinBase($sRez) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex($sRez) & @CRLF
                Case "bitrotate"
                    $aTmp = _SplitParams($aInput[1])
                    If UBound($aTmp) = 2 Then
                        ReDim $aTmp[3]
                        $aTmp[2] = 1
                    EndIf
                    If Execute($aTmp[2]) > 0 Then
                        $sTmp = $sOp & " left by " & Execute($aTmp[2])
                    ElseIf Execute($aTmp[2]) < 0 Then
                        $sTmp = $sOp & " right by " & Abs(Execute($aTmp[2]))
                    Else
                        $sTmp = $sOp & " by 0"
                    EndIf
                    If $aTmp[0] < 3 Then $sSize = ' ("W" within 16 bits Default)' ; Default
                    If $aTmp[0] = 3 Then
                        If $aTmp[3] = '"D"' Then $sSize = ' ("D" within 32 bits)'
                        If $aTmp[3] = '"W"' Then $sSize = ' ("W" within 16 bits)'
                        If $aTmp[3] = '"B"' Then $sSize = ' ("B" within 8 bits)'
                    EndIf
                    $sCalc = _HexToBinBase(Execute($aTmp[1])) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex(Execute($aTmp[1])) & @CRLF & _
                            $sTmp & $sSize & @CRLF & _
                            "=" & @CRLF & _
                            _HexToBinBase($sRez) & @TAB & @TAB & @TAB & @TAB & @TAB & @TAB & '   0x' & Hex($sRez) & @CRLF
                Case Else
                    $sError = "Not a bitwise operation."
            EndSwitch
        EndIf
    EndIf
    GUICtrlSetData($cResult, $sRez)
    If $sError <> "" Then
        GUICtrlSetData($cCalc, $sError)
    Else
        GUICtrlSetData($cCalc, $sCalc)
    EndIf
EndFunc   ;==>_BitWisor

#cs _HexToBinBase()
    Converts a number to binary base string
#ce
Func _HexToBinBase($hex)
    Local $b = ""
    For $i = 1 To 32
        $b = BitAND($hex, 1) & $b
        $hex = BitShift($hex, 1)
    Next
    Return StringRegExpReplace($b, "(.{8})", "1 ")
EndFunc   ;==>_HexToBinBase

#cs _SplitParams()
    Crappy AutoIt param string parser
    Should choke on quoted commas
    Returns StringSplit-like array
#ce
Func _SplitParams($sParams)
    Local $aParams, $i, $br1, $br2, $aRet[1]
    $aParams = StringSplit($sParams, ",")
    For $i = $aParams[0] To 1 Step -1
        StringReplace($aParams[$i], ")", "")
        $br1 = @extended
        StringReplace($aParams[$i], "(", "")
        $br2 = @extended
        If $br1 <> $br2 Then
            $aParams[$i - 1] &= "," & $aParams[$i]
            $aParams[$i] = ""
        EndIf
    Next
    For $i = 1 To $aParams[0]
        If $aParams[$i] <> "" Then
            ReDim $aRet[UBound($aRet) + 1]
            $aRet[UBound($aRet) - 1] = $aParams[$i]
        EndIf
    Next
    $aRet[0] = UBound($aRet) - 1
    Return $aRet
EndFunc   ;==>_SplitParams

#cs _GraphicDrawLines()
    Draws lines, duh
    Params: $cID - control ID from GuiCtrlCreateGraphic
    $aCoords - 1D array containing coordinate pairs ( $a[0]=x0, $a[1]=y0, $a[2]=x1, $a[3]=y1, etc.)
    $iPenSize, $iColor, $iColorBk - line options
#ce
Func _GraphicDrawLines($cID, $aCoords, $iPenSize = 2, $iColor = 0, $iColorBk = -1)
    GUICtrlSetGraphic($cID, $GUI_GR_PENSIZE, $iPenSize)
    GUICtrlSetGraphic($cID, $GUI_GR_COLOR, $iColor, $iColorBk)
    GUICtrlSetGraphic($cID, $GUI_GR_MOVE, $aCoords[0], $aCoords[1])
    For $i = 2 To UBound($aCoords) - 1 Step 2
        GUICtrlSetGraphic($cID, $GUI_GR_LINE, $aCoords[$i], $aCoords[$i + 1])
    Next
EndFunc   ;==>_GraphicDrawLines
;

Edit: Declared $sSize variable before use; and,

Made second parameter in BitRotate() have a default value equal to one (1).

Edited by Malkey
Link to comment
Share on other sites

  • 11 months later...

@Arm

Glad you brought up this old thread because I had lost track of it, in fact I've also lost track of Siao.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...