Sign in to follow this  
Followers 0
CountyIT

Need help with bit fields in a dll structure

4 posts in this topic

I am working on converting Microsoft's MTTTY routines over to AutoIt. I am stuck on bit fields in a dll structure. The field is a DWORD I have called "long fBitfields;" in the structure. The bit fields are:

fBinary (1 bit)

fParity (1 bit)

fOutxCtsFlow (1 bit)

fOutxDsrFlow(1 bit)

fDtrControl (2 bits)

fDsrSensitivity (1 bit)

fTXContinueOnXoff (1 bit)

fOutX (1 bit)

fInX (1 bit)

fErrorChar (1 bit)

fNull (1 bit)

fRtsControl (2 bits)

fAbortonerror (1 bit)

fDummy2 (17 bits)

for a total of 32 bits. Since AutoIt doesn't have a "bit" datatype I am a little lost on how to break the "long fBitfields" in to the variables above and when done setting those variables merging them back in to one "long fBitfields" so I can put them back in the structure.

Share this post


Link to post
Share on other sites



Use BitAnd, BitOr, BitNot, BitXor, ... as needed.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Can you maybe give me a little more detail? Is there and easy, self documented way like BitAnd(0x11111111111111111111111111111111, 0x11101000111111110001010111111111)?

Share this post


Link to post
Share on other sites

Here are some relevant examples using bit functions with a bitfield.

You may find this by Siao helpful.

#cs
fBinary (1 bit)
fParity (1 bit)
fOutxCtsFlow (1 bit)
fOutxDsrFlow(1 bit)
fDtrControl (2 bits)
fDsrSensitivity (1 bit)
fTXContinueOnXoff (1 bit)
fOutX (1 bit)
fInX (1 bit)
fErrorChar (1 bit)
fNull (1 bit)
fRtsControl (2 bits)
fAbortonerror (1 bit)
fDummy2 (17 bits)
#ce

; Note:- The function Bin2Dec() is only used to display the bit function results in a visual binary format (base 2).

Local $fBinary = 1
Local $fParity = 0
Local $fOutxCtsFlow = 1
Local $fOutxDsrFlow = 0
Local $fDtrControl = 3 ; = 11b
Local $fDsrSensitivity = 0
Local $fTXContinueOnXoff = 1
Local $fOutX = 0
Local $fInX = 1
Local $fErrorChar = 0
Local $fNull = 1
Local $fRtsControl = 0 ; = 00b
Local $fAbortonerror = 1
Local $fDummy2 = 0xFFFE ; = 1111111111111110b

Local $BitField = BitOR( _
BitShift($fDummy2, -15), _           ; 16 bits
BitShift($fAbortonerror, -14), _         ; or BitRotate($fAbortonerror, 14, "D"), _
BitRotate($fRtsControl, 12, "D"), _  ; 2 bits
BitRotate($fNull, 11, "D"), _
BitRotate($fErrorChar, 10, "D"), _
BitRotate($fInX, 9, "D"), _          ; or $fInX          * (2 ^ 9), _
BitRotate($fOutX, 8, "D"), _             ; or $fOutX             * (2 ^ 8), _
BitRotate($fTXContinueOnXoff, 7, "D"), _ ; or $fTXContinueOnXoff * (2 ^ 7), _
$fDsrSensitivity * (2 ^ 6), _
$fDtrControl     * (2 ^ 4), _        ; 2 bits
$fOutxDsrFlow    * (2 ^ 3), _
$fOutxCtsFlow    * (2 ^ 2), _
$fParity         * (2 ^ 1), _
$fBinary         * (2 ^ 0) _
)

ConsoleWrite("The bitfield: " & $BitField & " = 0x" & Hex($BitField, 8) & " = " & Dec2Bin($BitField) & @LF)
ConsoleWrite(Bin2Dec("1111111111111110100101010110101") & @LF)
;Returns:-
; The bitfield: 2147437237 = 0x7FFF4AB5 = 1111111111111110100101010110101
; 2147437237

; --------------------------------------------------------------------------------------------------------------
ConsoleWrite("=============================" & @LF)

$BitField = 2147437237
ConsoleWrite(Dec2Bin($BitField) & " (Notice 5th & 6th bits are 1's or 3"& @LF) ; Returns 1111111111111110100101010110101 (Notice 5th & 6th bits are 1's or 3)

ConsoleWrite( BitAnd($BitField, 3 * (2 ^ 4)) & @LF) ; Returns 48 which equals 0x30 (hexadecimal)
If BitAnd($BitField, 3 * (2 ^ 4)) = 0x30 Then
$BitField = Bitxor($BitField, 3 * (2 ^ 4)) ; Returns 1111111111111110100101010000101 (Notice 5th & 6th bits are zero)
ConsoleWrite(Dec2Bin($BitField) & " (Notice 5th & 6th bits are zero)" & @LF)
EndIf

ConsoleWrite( BitAnd($BitField, 3 * (2 ^ 4)) & @LF) ; Returns 0
If BitAnd($BitField, 3 * (2 ^ 4)) = 0 Then
$BitField = Bitxor($BitField, 3 * (2 ^ 4))
ConsoleWrite(Dec2Bin($BitField) & " (Notice 5th & 6th bits are 1's or 3" & @LF) ; Returns 1111111111111110100101010110101 (Notice 5th & 6th bits are 1's or 3)
EndIf
;Returns:-
;1111111111111110100101010110101
;48
;1111111111111110100101010000101
;0
;1111111111111110100101010110101

; --------------------------------------------------------------------------------------------------------------
ConsoleWrite("=============================" & @LF)

ConsoleWrite(Dec2Bin(BitOr($BitField, 1 * 2 ^ 3)) & " Set 4th bit, $fOutxDsrFlow = 1" & @LF) ; Make 4th bit, $fOutxDsrFlow = 1
ConsoleWrite(Dec2Bin(BitAnd($BitField, BitNot(2 ^ 3))) & " Set 4th bit, $fOutxDsrFlow = 0" & @LF) ; Make 4th bit, $fOutxDsrFlow = 0

$BitField = BitSet($BitField, 5, 2, 2)
ConsoleWrite(Dec2Bin($BitField) & " Set $fDtrControl = 10b or 2 (5th & 6th bits)" & @LF) ; Make $fDtrControl = 10b or 2

ConsoleWrite(('"' & BitGet($BitField, 5, 2)) & '" is the 2 bit value in the 5th and 6th bits of the bitfield.' & @LF)

;Returns:-
;1111111111111110100101010111101 Set 4th bit, $fOutxDsrFlow = 1
;1111111111111110100101010110101 Set 4th bit, $fOutxDsrFlow = 0
;1111111111111110100101010100101 Set $fDtrControl = 10b or 2 (5th & 6th bits)
;"2" is the 2 bit value in the 5th and 6th bits of the bitfield.


; For use with a 32 bit long bitfield
Func BitSet($iBitField, $iBitPosition, $iValue, $iBitSize = 1)
If Int(Log($iValue) / Log(2)) + 1 > $iBitSize Then
MsgBox(16, "Error", 'Value: "' & $iValue & '" will not fit within the bit size of "' & $iBitSize & '".')
Return SetError(1, "", 1)
EndIf
Return SetError(0, "", _
BitOR(Bitshift(Bitshift($iBitField, $iBitPosition + 1), - $iBitPosition - 1), _
     $iValue * 2 ^ ($iBitPosition - 1), _
Bitshift(Bitshift($iBitField, - 32 + ($iBitPosition - 1)), 32 - ($iBitPosition - 1)) _
))
EndFunc

; $iBitPosition - Bit position one is first bit at right.
; $iSize - Number of bits to read.
; Return - Decimal value. If one bit read, returns 1 or 0. If two bits read, returns either 0, 1, 2, or 3. And so on.
Func BitGet($iBitField, $iBitPosition, $iSize = 1)
Return BitShift(BitAnd($iBitField,( - 1 + 2 ^$iSize) * 2 ^ ($iBitPosition - 1)), $iBitPosition - 1)
EndFunc

Func Bin2Dec($iN)
Local $aN, $iBin = 0
$aN = StringSplit($iN, "", 1)
For $x = 1 To UBound($aN) - 1
$iBin += $aN[$x] * (2 ^ (UBound($aN) - 1 - $x))
Next
Return $iBin
EndFunc ;==>Bin2Dec

Func Dec2Bin($iN)
Local $iRemainder, $iBin = ""
Do
$iBin = Mod($iN, 2) & $iBin
$iN = Floor($iN / 2)
Until $iN = 0
Return $iBin
EndFunc ;==>Dec2Bin

Share this post


Link to post
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
Sign in to follow this  
Followers 0