Date: 02-16-2022
Return to Index
created by gbSnippets
'The bits that make up numeric variables can be individually set. This means
'a 32-bit variable, can pass 32 different pieces of information, usually called Flags.
'The bit-wise operators Or, AND, Not, and XOR are used to perform bit manipulation.
'Primary Code:
MyVar = MyVar Or Mask 'Set a bit
MyVar = MyVar AND (Not Mask) 'Clear a bit
MyVar = MyVar XOR Mask 'Toggle a bit
iResult = MyVar AND Mask 'Read a bit
'To Set a bit (set bit to one):
MyVar = MyVar Or 2^4 'sets bit 4 to 1 MyVar OR 8
MyVar = MyVar Or 2^0 Or 2^4 'sets both bits 0/4 MyVar OR 1 OR 5
MyVar = MyVar Or (2^0 + 2^4) 'sets both bits 0/4 MyVar OR 1 OR 5
'To Clear Bits in the variable (set bit to zero)
MyVar = MyVar AND (Not 2^1) 'clears bit 1 (set to zero)
MyVar = MyVar AND (Not 2^4) AND (Not 2^6) 'clears bits 4 and 5 (both set to zero)
'Read a variable bit
iResult = MyVar AND 2^1 'returns 1 if bit 1 is set, otherwise returns 0
iResult = MyVar AND 2^4 'returns 1 if bit 4 is set, otherwise returns 0
'Toggle a variable bit
MyVar = MyVar XOR 2^4 'bit 4 is toggled
MyVar = MyVar XOR 2^0 XOR 2^3 'bits 0 and 3 are toggled
MyVar = MyVar XOR (2^0 + 2^3) 'bits 0 and 3 are toggled
'Compilable Example: (Jose Includes)
'This example shows how AND and OR are often used in PowerBASIC apps.
'OR is used to set flags for the MsgBox statement
'AND is used to test for a flag in the wParam of the %WM_SysCommand message
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,220, %WS_OverlappedWindow To hDlg
Control Add Label, hDlg, 100,"Exit the app to see what happens.", 20,10,180,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local Style&
Select Case CB.Msg
Case %WM_SYSCOMMAND
If (CB.wParam AND &HFFF0) = %SC_Close Then 'trap Alt-F4 and X Button
Style& = %MB_OkCancel Or %MB_IconQuestion Or %MB_TaskModal
Select Case MsgBox("Exit the app?", Style&, "Close Application")
Case %IdYes
Function = 0 'Save, then destroy
Case %IdCancel
Function = 1 'True - abort the close - no further processing needed
End Select
End If
End Select
End Function
'gbs_00198
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm