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
Local $Bin = ''
Local $Zahl8 = -128
Local $Zahl16 = -32768
;~ Local $Zahl32 = -2147483648
Local $Zahl32 = 2147483647
For $i = 1 To 8
$Bin &= _GetBit8($Zahl8, $i)
Next
MsgBox(0, $Zahl8, $Bin)
$Bin = ''
For $i = 1 To 16
$Bin &= _GetBit16($Zahl16, $i)
Next
MsgBox(0, $Zahl16, $Bin)
$Bin = ''
For $i = 1 To 32
$Bin &= _GetBit32($Zahl32, $i)
Next
MsgBox(0, $Zahl32, $Bin)
Func _GetBit32($iZahl, $iBit) ;für Doppelworte
Local $1, $2, $3, $4
Local $Bits = _Int2Bits32($iZahl)
$1 = StringLeft($Bits,
$2 = StringTrimLeft(StringLeft($Bits, 16),
$3 = StringTrimRight(StringRight($Bits, 16),
$4 = StringRight($Bits,
$Bits = StringTrimRight($4&$3&$2&$1, $iBit - 1)
If $Bits = "" Then Return 0
Return StringRight($Bits, 1)
EndFunc
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
Func _GetBit16($iZahl, $iBit) ;für Worte
Local $1, $2
Local $Bits = _Int2Bits16($iZahl)
$1 = StringLeft($Bits,
$2 = StringRight($Bits,
$Bits = StringTrimRight($2&$1, $iBit - 1)
If $Bits = "" Then Return 0
Return StringRight($Bits, 1)
EndFunc
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
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
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