
GetBit(dw.l, nBitNumber) - gets a n-bit at number (the first bit as 0)
SetBit(*_dw, nBitNumber, nBitValue) - sets a n-bit at number (the first bit as 0)
GetBinDWord(dw.l) - works like a Bin()
The example is in a code.
Code: Select all
; BitHelper.pbi
; by Gemorg
Procedure GetBit(dw.l, nBitNumber)
; dw - целое в котором узнаем бит
; nBitNumber - номер бита, который узнаем (0..31)
Protected dwMask = 1 << nBitNumber
dw = dwMask & dw;
If(dw)
ProcedureReturn 1
EndIf
ProcedureReturn 0
EndProcedure
Procedure SetBit(*_dw, nBitNumber, nBitValue)
; dw - целое, в котором задаем бит
; nBitNumber - номер бита, который задаем (0..31)
Protected *dw.Long = *_dw
Protected dwMask = 1 << nBitNumber
If(nBitValue = 0)
; задаем 0
dwMask = ~dwMask; 1111011111....
*dw\l = *dw\l & dwMask; 0 & x = 0
Else
; задаем 1
*dw\l = *dw\l | dwMask; 1 | x = 1
EndIf
EndProcedure
Procedure.s GetBinDWord(dw.l)
Protected res.s = "", i
For i = 0 To 31
res + Str(GetBit(dw, i))
Next
res = ReverseString(res)
ProcedureReturn res
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
x = 3
Debug "Decimal value: " + x
Debug "Bin value by Bin():"
Debug RSet(Bin(x, #PB_Long), 32, "0")
Debug "Bin value by GetBinDWord():"
Debug GetBinDWord(x)
Debug #CRLF$
Debug "Set zero bit to 0..."
SetBit(@x, 0, 0)
Debug "Decimal value: " + x
Debug "Bin value by Bin():"
Debug RSet(Bin(x, #PB_Long), 32, "0")
Debug "Bin value by GetBinDWord():"
Debug GetBinDWord(x)
Debug #CRLF$
Debug "FR overflow test:"
flag.l = 0
! MOV [v_flag], 2147483647 ; Long - 32 bit, max value - 2147483647
! ADD [v_flag], 1 ; Overflow
! PUSHFD ; push Flag Register
! POP EAX
! MOV [v_flag], EAX
Debug GetBinDWord(flag)
Debug "CF: " + GetBit(flag, 0)
Debug "PF: " + GetBit(flag, 2)
Debug "AF: " + GetBit(flag, 4)
Debug "ZF: " + GetBit(flag, 6)
Debug "SF: " + GetBit(flag, 7)
Debug "TF: " + GetBit(flag, 8)
Debug "IF: " + GetBit(flag, 9)
Debug "OF: " + GetBit(flag, 11) ; must be 1
Debug "NT: " + GetBit(flag, 14)
CompilerEndIf

p.s. sorry for my english