'===========================================================================
' Subject: BIT MANIPULATION FUNCTIONS         Date: 07-13-92 (00:00)       
'  Author: Luis Espinoza                      Code: QB, QBasic, PDS        
'  Origin: FidoNet QUIK_BAS Echo            Packet: BINARY.ABC
'===========================================================================
'> > Ok, this is an extremely useful internal PB command, one of many
'> > such as shift right, shift left, bit toggle, and so forth which
'> > obviously affects bits of bytes.  In my code, the background [...]
'>
'> I'll give some thought to shift right this
'> afternoon and see what I come up with.

'Follows are a few bit manipulation FUNCTIONs posted by Luis Espinoza
'here a few years ago - they include FUNCTIONs LShift%() and RShift%()
'which I believe are the equivalent of the SHIFTR/SHIFTL:

DEFINT A-Z

DECLARE FUNCTION LShift% (Byte%, Bits%)
DECLARE FUNCTION ReadBit% (Byte%, Bit%)
DECLARE FUNCTION ResetBit% (Byte%, Bit%)
DECLARE FUNCTION RShift% (Byte%, Bits%)
DECLARE FUNCTION SetBit% (Byte%, Bit%)
DECLARE FUNCTION ToggleBit% (Byte%, Bit%)

'***********************************************************************
'* FUNCTION LShift%
'*
'* PURPOSE
'*    Shifts Byte% left by specified number of Bits%.
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-13-92.
'***********************************************************************
FUNCTION LShift% (Byte%, Bits%) STATIC
   LShift% = (Byte% * (2 ^ Bits%)) MOD 256
END FUNCTION

'***********************************************************************
'* FUNCTION ReadBit%
'*
'* PURPOSE
'*    Returns bit specified by Bit% (0-7) in Byte% (0-32767).
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-04-92.
'***********************************************************************
FUNCTION ReadBit% (Byte%, Bit%) STATIC
   ReadBit% = ABS(((2 ^ (Bit%)) AND Byte%) > 0)
END FUNCTION

'***********************************************************************
'* FUNCTION ResetBit%
'*
'* PURPOSE
'*    Resets the bit in Byte% specified by Bit%.
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-13-92.
'***********************************************************************
FUNCTION ResetBit% (Byte%, Bit%) STATIC
   ResetBit% = Byte% AND (255 - (2 ^ Bit%))
END FUNCTION

'***********************************************************************
'* FUNCTION RShift%
'*
'* PURPOSE
'*    Shifts Byte% right by specified number of Bits%.
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-13-92.
'***********************************************************************
FUNCTION RShift% (Byte%, Bits%) STATIC
   RShift% = Byte% \ (2 ^ Bits%)
END FUNCTION

'***********************************************************************
'* FUNCTION SetBit%
'*
'* PURPOSE
'*    Sets the bit in Byte% specified by Bit%.
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-13-92.
'***********************************************************************
FUNCTION SetBit% (Byte%, Bit%) STATIC
   SetBit% = Byte% OR (2 ^ Bit%)
END FUNCTION

'***********************************************************************
'* FUNCTION ToggleBit%
'*
'* PURPOSE
'*    Toggles the bit in Byte% specified by Bit%.
'*
'* CREDIT(S)
'*    Luis Espinoza, RIME QuickBASIC conference, 07-13-92.
'***********************************************************************
FUNCTION ToggleBit% (Byte%, Bit%) STATIC
   ToggleBit% = Byte% XOR (2 ^ Bit%)
END FUNCTION
