How to Store Multiple "Flags" in One LONG

  MAP
      BNOT(LONG pBitmap, LONG pMask),LONG
  END

BNOT    PROCEDURE  (LONG pBitmap, LONG pMask) 
  CODE
  RETURN BXOR(pBitMap,(BAND(pBitMap,pMask)))        

Thank you very much Jeff!!

Jeff’s way works but the more traditional way is a “NOT AND” or “AND Complement”

Val2 = BAND( Value1, BXOR( BitsOffMask , 0FFFFFFFFh) )
  or
Val2 = BAND( Value1, BXOR( BitsOffMask , -1 ) )        !LONG -1 = 0FFFFFFFFh

I find this “And Complement” easy to remember.
The BXOR( Mask , 0FFFFFFFFh ) is the “1’s Complement” which “Flips” all the Bits.
So the 1 bit in the Off Mask is now 0, and all the 0’s are 1.
Then BAND( Value1, Complement Mask ) leaves as 1 all Bits that were =1, but that 0.


Example to Turn Off the Font Style Italic bit

FONT:Regular   EQUATE (400)     ! 10010000b   0190h 
FONT:Weight    EQUATE (07FFH)
FONT:Italic    EQUATE (01000H)  ! 00010000 00000000b

Use this BAND( Style, BXOR( Font:Italic, -1))

First Complement the Bits to Turn Off which set All Bits as 1 but that single 10000h bit
BXOR( FONT:Italic , 0FFFFFFFFh)
-----------------> = FFFFEFFFh = 11111111 11111111 11101111 11111111b

Then AND(, Mask Complement) returns all but the Italic bit: BAND( Style, 0FFFFEFFFh )

Then BAND( Style, 0FFFFEFFFh ) i.e. AND(, Mask Complement) returns all but the Italic bit.

In a Signed LONG Integer a -1 is 0FFFFFFFFh so this can be used for less code as BXOR(,-1) to complement. Clarion does not have a Bitwise NOT operator.

1 Like

Added on GitHub an old tool Bit Mapper that takes a LONG and shows it split into all the Bits, Nibbles and Bytes.

I made it for working with Windows API functions like GetWindowLong() where the Styles have a lot of bit flags. It can take hex constants in C/C++/C# style 0x123ABC as you would find in the SDK .H files. Also VB style &H123ABC since there are many API examples in VB6.


You can test Clarion Bitwise functions BAND BOR BXOR BSHIFT to see the results.

The “~AND” button is an AND Complement to turn off bits. The Plus / Minus can show how they are not Bit Operations and can mess up your map.


4 Likes

That looks Neat-O and useful.

1 Like

Very nice Carl!! I look forward to checking it out!