Jump to content

BitAnd


Mpro
 Share

Recommended Posts

#include <string.au3>
$Temp = 3667563225088

$Temp1 = _BitAnd($Temp, 4294967295)
$Temp2 = _BitXOR($Temp, 1)
Msgbox(0,"Result", "BitAnd = " & $Temp1 & @LF & "BitXOR = " & $Temp2, 0)

;--------------------------------------------------------------------------------------------
; Function _BitAnd
; Simulate the BitAND function in AutoIt!
;--------------------------------------------------------------------------------------------
; Input     :   Decimal number
;               Decimal Mask
; Output    :   Decimal result
;--------------------------------------------------------------------------------------------
; Conversion Decimal to Binary for the first decimal number (Value)
; Conversion Decimal to Binary for the second decimal number (Mask)
; Reverse the Binary value
; Pad the shortest string with 0
; Gate And
; Convert the Binary result to decimal number
;--------------------------------------------------------------------------------------------
Func _BitAnd($arg1,$arg2)
    Local $bin = "", $bin2 = "", $result = ""

    $bin = _Dec2Bin($arg1)
    $bin2 = _Dec2Bin($arg2)
;-----------------------------------------------
; Reverse the Binary'3 String
; Before    = 00000000000001011011001111010111
; After     = 11101011110011011010000000000000
;-----------------------------------------------
    $bin  = _StringReverse($bin)
    $bin2 = _StringReverse($bin2)

;-----------------------------------------------
; Pad the shortest string with 0 based on the longest one
; EX:   Before padding = 11111111111111111111111111111111
;       After Padding  = 000000000011111111111111111111111111111111
;-----------------------------------------------
    If StringLen($bin) <> StringLen($bin2) Then
        If StringLen($bin) > StringLen($bin2) Then
            $add =""
            For $i = 1 To (StringLen($bin) - StringLen($bin2)) Step 1
                $add  &= 0
            Next
            $bin2 = $add & $bin2
        Else
            $add = ""
            For $i = 1 To (StringLen($bin2) - StringLen($bin)) Step 1
                $add  &= 0
            Next
            $bin = $add & $bin
        EndIf
    EndIf

;-----------------------------------------------
; AND Gate
;INPUT  OUTPUT
;A   B  A AND B
;0   0     0
;0   1     0
;1   0     0
;1   1     1
;-----------------------------------------------
    $split1 = StringSplit($bin,"")
    $split2 = StringSplit($bin2,"")
    For $i = 1 To StringLen($bin) Step 1
        ; Pour la fonction AND si un ou l'autre est 0 le résultat est 0
        If $split1[$i] = 0  Or $split2[$i] = 0 Then
            $result &= 0
        ; Si les deux sont à 1 alors le résultat est 1
        ElseIf $split1[$i] = $split2[$i] And $split1[$i] = 1 Then
            $result &= 1
        EndIf
    Next

;-----------------------------------------------
; Convert the Binary result to decimal number
;-----------------------------------------------
    $split = StringSplit($result, "")
    $x = $split[1]
    For $i = 1 To StringLen($result)-1 Step 1
        $x = $x * 2 + $split[$i+1]
    Next
    $result = $x
    Return $result
EndFunc

;--------------------------------------------------------------------------------------------
; Function _BitXOR
;Simulate the BitXOR function in AutoIt!
;--------------------------------------------------------------------------------------------
; Input     :   Decimal number
;               Decimal Mask
; Output    :   Decimal result
;--------------------------------------------------------------------------------------------
; Conversion Decimal to Binary for the first decimal number (Value)
; Conversion Decimal to Binary for the second decimal number (Mask)
; Reverse the Binary value
; Pad the shortest string with 0
; Gate XOR
; Convert the Binary result to decimal number
;--------------------------------------------------------------------------------------------
Func _BitXOR($arg1,$arg2)
    Local $bin = "", $bin2 = "", $result = ""

    $bin = _Dec2Bin($arg1)
    $bin2 = _Dec2Bin($arg2)
;-----------------------------------------------
; Reverse the Binary'3 String
; Before    = 00000000000001011011001111010111
; After     = 11101011110011011010000000000000
;-----------------------------------------------
    $bin  = _StringReverse($bin)
    $bin2 = _StringReverse($bin2)

;-----------------------------------------------
; Pad the shortest string with 0 based on the longest one
; EX:   Before padding = 11111111111111111111111111111111
;       After Padding  = 000000000011111111111111111111111111111111
;-----------------------------------------------
    If StringLen($bin) <> StringLen($bin2) Then
        If StringLen($bin) > StringLen($bin2) Then
            $add =""
            For $i = 1 To (StringLen($bin) - StringLen($bin2)) Step 1
                $add  &= 0
            Next
            $bin2 = $add & $bin2
        Else
            $add = ""
            For $i = 1 To (StringLen($bin2) - StringLen($bin)) Step 1
                $add  &= 0
            Next
            $bin = $add & $bin
        EndIf
    EndIf

;-----------------------------------------------
; Code de la fonction XOR
; Décortique les valeures binaires
; XOR Gate
;INPUT  OUTPUT
;A   B  A XOR B
;0   0     0
;0   1     1
;1   0     1
;1   1     0
;-----------------------------------------------
$split1 = StringSplit($bin,"")
$split2 = StringSplit($bin2,"")
For $i = 1 To StringLen($bin) Step 1
; Pour la fonction XOR si le chiffre est différent le résultat est 1
    If $split1[$i] <> $split2[$i] Then
        $result &= 1
    Else
        $result &= 0
    EndIf
Next

;-----------------------------------------------
; Convert the Binary result to decimal number
;-----------------------------------------------
    $split = StringSplit($result, "")
    $x = $split[1]
    For $i = 1 To StringLen($result)-1 Step 1
        $x = $x * 2 + $split[$i+1]
    Next
    $result = $x
    Return $result
EndFunc

;--------------------------------------------------------------------------------------------
; Function _Dec2Bin
; Decimal to binary conversion
;--------------------------------------------------------------------------------------------
; Input     : Decimal number
; Output    : Binary Number
;--------------------------------------------------------------------------------------------
; Convert by divisions
; Reverse the binary number
; Value Conversion (Decimal to Binary)
; Ex: Decimal = 3667563225088 to binary = 00000000000001011011001111010111 (inversed value)
;--------------------------------------------------------------------------------------------
Func _Dec2Bin($arg)
    Local $result = ""
    While($arg >= 1)
        $arg /= 2
        If StringIsDigit($arg) Then
            $result &= 0
        Else
            $split = StringSplit($arg, ".")
            $arg = $split[1]
            $result &= 1
        EndIf
    WEnd
    Return $result
EndFunc

Link to comment
Share on other sites

a little modification:

$Temp = 3667563225088
$Temp1 = _Bit(1, $Temp, "0xFFFFFFFF");4294967295)
Msgbox(0,"Result of bit operations", "BitRes = " & $Temp1)

;--------------------------------------------------------------------------------------------
; Function _Bit
; Simulate the BitAND, BitXOR & BitOR function in AutoIt!
;--------------------------------------------------------------------------------------------
;    first argument:
;   [argument] [action]
;       1       BitAND
;       2       BitXOR
;       3       BitOR
; Input     :   Decimal or "Hex" number
;               Decimal or "Hex" Mask
; Output    :   Decimal result
;--------------------------------------------------------------------------------------------
; Conversion Decimal to Binary for the first decimal number (Value)
; Conversion Decimal to Binary for the second decimal number (Mask)
; Reverse the Binary value
; Pad the shortest string with 0
; Do specified action
; Convert the Binary result to decimal number
;--------------------------------------------------------------------------------------------
Func _Bit($task, $arg1, $arg2)
    Local $bin = "", $bin2 = "", $result = ""

If StringInStr($arg1, "x") Then
    $bin = _Hex2Bin($arg1)
Else
    $bin = _Dec2Bin($arg1)
EndIf

If StringInStr($arg2, "x") Then
    $bin2 = _Hex2Bin($arg2)
Else
    $bin2 = _Dec2Bin($arg2)
EndIf
;-----------------------------------------------
; Reverse the Binary'3 String
; Before    = 00000000000001011011001111010111
; After     = 11101011110011011010000000000000
;-----------------------------------------------
    $bin  = _Reverse($bin)
    $bin2 = _Reverse($bin2)
;-----------------------------------------------
; Pad the shortest string with 0 based on the longest one
; EX:   Before padding = 11111111111111111111111111111111
;       After Padding  = 000000000011111111111111111111111111111111
;-----------------------------------------------
    If StringLen($bin) <> StringLen($bin2) Then
        If StringLen($bin) > StringLen($bin2) Then
            $add =""
            For $i = 1 To (StringLen($bin) - StringLen($bin2)) Step 1
                $add  &= 0
            Next
            $bin2 = $add & $bin2
        Else
            $add = ""
            For $i = 1 To (StringLen($bin2) - StringLen($bin)) Step 1
                $add  &= 0
            Next
            $bin = $add & $bin
        EndIf
    EndIf


    $split1 = StringSplit($bin,"")
    $split2 = StringSplit($bin2,"")

Switch $task
    Case 1
        For $i = 1 To StringLen($bin) Step 1
            If $split1[$i] = 0  Or $split2[$i] = 0 Then
                $result &= 0
            ElseIf $split1[$i] = $split2[$i] And $split1[$i] = 1 Then
                $result &= 1
            EndIf
        Next
    Case 2
        For $i = 1 To StringLen($bin) Step 1
            If $split1[$i] <> $split2[$i] Then
                $result &= 1
            Else
                $result &= 0
            EndIf
        Next
    Case 3
        For $i = 1 To StringLen($bin) Step 1
            If $split1[$i] = 1 Or $split2[$i] = 1 Then
                $result &= 1
            Else
                $result &= 0
            EndIf
        Next

    EndSwitch
;-----------------------------------------------
; Convert the Binary result to decimal number
;-----------------------------------------------
    If StringLen($result)>=1 Then
    $split = StringSplit($result, "")
    $x = $split[1]

    For $i = 1 To StringLen($result)-1 Step 1
        $x = $x * 2 + $split[$i+1]
    Next
    $result = $x
    EndIf

    Return $result

EndFunc


;--------------------------------------------------------------------------------------------
; Function _Dec2Bin
; Decimal to binary conversion
;--------------------------------------------------------------------------------------------
; Input     : Decimal number
; Output    : Binary Number
;--------------------------------------------------------------------------------------------
; Convert by divisions
; Reverse the binary number
; Value Conversion (Decimal to Binary)
; Ex: Decimal = 3667563225088 to binary = 00000000000001011011001111010111 (inversed value)
;--------------------------------------------------------------------------------------------
Func _Dec2Bin($arg)
    Local $result = ""
    If $arg = 0 Then
        Return 0
    Else
    While($arg >= 1)
        $arg /= 2
        If StringIsDigit($arg) Then
            $result &= 0
        Else
            $split = StringSplit($arg, ".")
            $arg = $split[1]
            $result &= 1
        EndIf
    WEnd
    Return $result
    EndIf
EndFunc

Func _Hex2Bin($arg)
    Local $bin = ""
    Local $result = ""
    $result = StringReplace($arg, "0x", "")
For $i = 0 To 15 Step 1
    $bin = _Dec2Bin($i)
    Switch StringLen($bin)
        Case 1
            $bin = "000" & $bin
        Case 2
            $bin = "00" & $bin
        Case 3
            $bin = "0" & $bin
    EndSwitch
    $result = StringReplace($result, Hex($i, 1), $bin)
Next
    Return $result
EndFunc

Func _Reverse($arg)
    Local $result = ""
    $split = StringSplit($arg, "")
    For $i = StringLen($arg) To 1 Step -1
        $result &= $split[$i]
    Next
    Return $result
EndFunc

Changes: compacted, performs BitAND, BitXOR & BitOR, also allows to use hex-values, but with one bug: - it must be in "", String() not helps in this case, - i don't know why.

Edited by Godless

_____________________________________________________________________________

Link to comment
Share on other sites

Hi AutoIt developper!

Thanks Gedless for Compacted / completed the Bit Operations script!

It's true, you need "" around "0xFFFFFFFF" because in signed number 0xFFFFFFFF = -1.

"" Not need for positive number like 0x7FFFFFFF.

With your collaboration my script is done and do the job perfectly!!!! :mellow:

:(

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...