Grenzen Bit-Operationen für signed 32Bit-Integer

  • Hallo!

    Ich benötige Bitoperationen im Bereich von -2147483648 bis 2147483647. Demnach müsste ich mit Werten bis maximal 4294967295 rechnen können.
    Das funktioniert allerdings nicht mehr. Weiß einer wie man mit negativen Werten oder mit Werten über 2147483647 Bit-Operationen durchführen kann?

    Spoiler anzeigen
    [autoit]

    Local $Bin = ''
    Local $Zahl8 = -128
    Local $Zahl16 = -32768

    [/autoit] [autoit][/autoit] [autoit]

    ;~ Local $Zahl32 = -2147483648
    Local $Zahl32 = 2147483647

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To 8
    $Bin &= _GetBit8($Zahl8, $i)
    Next
    MsgBox(0, $Zahl8, $Bin)

    [/autoit] [autoit][/autoit] [autoit]

    $Bin = ''
    For $i = 1 To 16
    $Bin &= _GetBit16($Zahl16, $i)
    Next
    MsgBox(0, $Zahl16, $Bin)

    [/autoit] [autoit][/autoit] [autoit]

    $Bin = ''
    For $i = 1 To 32
    $Bin &= _GetBit32($Zahl32, $i)
    Next
    MsgBox(0, $Zahl32, $Bin)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _GetBit32($iZahl, $iBit) ;für Doppelworte
    Local $1, $2, $3, $4
    Local $Bits = _Int2Bits32($iZahl)
    $1 = StringLeft($Bits, 8)
    $2 = StringTrimLeft(StringLeft($Bits, 16), 8)
    $3 = StringTrimRight(StringRight($Bits, 16), 8)
    $4 = StringRight($Bits, 8)
    $Bits = StringTrimRight($4&$3&$2&$1, $iBit - 1)
    If $Bits = "" Then Return 0
    Return StringRight($Bits, 1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Int2Bits32($iZahl)
    If $iZahl > 2147483647 Then Return SetError(1, 0, 0)
    Local $Zahl = $iZahl, $Ergebnis = ""
    If $iZahl < 0 Then $Zahl = 4294967296 + $iZahl
    Do
    $Ergebnis = BitAND($Zahl, 1) & $Ergebnis
    $Zahl = BitShift($Zahl, 1)
    Until $Zahl = 0
    Return StringFormat('%032s', $Ergebnis)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetBit16($iZahl, $iBit) ;für Worte
    Local $1, $2
    Local $Bits = _Int2Bits16($iZahl)
    $1 = StringLeft($Bits, 8)
    $2 = StringRight($Bits, 8)
    $Bits = StringTrimRight($2&$1, $iBit - 1)
    If $Bits = "" Then Return 0
    Return StringRight($Bits, 1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Int2Bits16($iZahl)
    If $iZahl > 32767 Then Return SetError(1, 0, 0)
    Local $Zahl = $iZahl, $Ergebnis = ""
    If $iZahl < 0 Then $Zahl = 65536 + $iZahl
    Do
    $Ergebnis = BitAND($Zahl, 1) & $Ergebnis
    $Zahl = BitShift($Zahl, 1)
    Until $Zahl = 0
    Return StringFormat('%016s', $Ergebnis)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetBit8($iZahl, $iBit) ;für ein Byte
    Local $Bits = _Int2Bits8($iZahl)
    $Bits = StringTrimRight($Bits, $iBit - 1)
    If $Bits = "" Then Return 0
    Return StringRight($Bits, 1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Int2Bits8($iZahl)
    If $iZahl > 127 Then Return SetError(1, 0, 0)
    Local $Zahl = $iZahl, $Ergebnis = ""
    If $iZahl < 0 Then $Zahl = 256 + $iZahl
    Do
    $Ergebnis = BitAND($Zahl, 1) & $Ergebnis
    $Zahl = BitShift($Zahl, 1)
    Until $Zahl = 0
    Return StringFormat('%008s', $Ergebnis)
    EndFunc

    [/autoit]
  • helfen kann ich dir da nich ^^ ich kann dir nur sagen was ich ma ghört habe: Du sagst ja das des mit der Zahl die du hast nicht funktioniert... dies liegt womöglcih daran, dass du "an die genzen von AuoIt" gekommen bist... diese kann man (so hab ichs gehört) verändern, undzwar mit irgendwelchen komischen variabeln... kp ob des stimmt...
    Hoffe das Hilft dir

    MFg Scripter192

  • Der_Doc: Da hab ich auch mitgemacht bei dem Wettbewerb. Mein Skript war sogar das drittschnellste. ;)

    Ich habs jetzt so gelöst, aber eigentlich hatte ich gedacht, dass man die Bitoperationen bis 2³² durchführen kann, habe mich wohl getäuscht.

    Spoiler anzeigen
    [autoit]

    Local $Bin = ''

    [/autoit] [autoit][/autoit] [autoit]

    Local $Zahl32 = -1
    ;~ Local $Zahl32 = 2147483647

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To 32
    $Bin &= _GetBit32($Zahl32, $i)
    Next
    MsgBox(0, $Zahl32, $Bin)

    [/autoit] [autoit][/autoit] [autoit]

    Func _GetBit32($iZahl, $iBit) ;für Doppelworte
    Local $1, $2, $3, $4
    Local $Bits = _Int2Bits32($iZahl)
    $1 = StringLeft($Bits, 8)
    $2 = StringTrimLeft(StringLeft($Bits, 16), 8)
    $3 = StringTrimRight(StringRight($Bits, 16), 8)
    $4 = StringRight($Bits, 8)
    $Bits = StringTrimRight($4&$3&$2&$1, $iBit - 1)
    If $Bits = "" Then Return 0
    Return StringRight($Bits, 1)
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _Int2Bits32($iZahl)
    If $iZahl > 2147483647 Then Return SetError(1, 0, 0)
    Local $Zahl = $iZahl, $Ergebnis = ""
    If $iZahl < 0 Then $Zahl = 2147483648 + $iZahl
    Do
    $Ergebnis = BitAND($Zahl, 1) & $Ergebnis
    $Zahl = BitShift($Zahl, 1)
    Until $Zahl = 0
    $Ergebnis = StringFormat('%032s', $Ergebnis)
    If $iZahl < 0 Then $Ergebnis = '1' & StringTrimLeft($Ergebnis, 1)
    Return $Ergebnis
    EndFunc

    [/autoit]