An interesting example of this in the Clarion language are the Font Style attributes are a bitmap stored in LONG that is used by FONT(), GETFONT(), SETFONT()
These are in Equates.CLW
FONT:Thin EQUATE (100)
FONT:Regular EQUATE (400)
! Semi Bold EQUATE(600) !From Win API
FONT:Bold EQUATE (700)
! Extra Bold EQUATE(800) !From Win API
! Black EQUATE(900) !From Win API
FONT:Weight EQUATE (07FFH)
FONT:Fixed EQUATE (0800H)
FONT:Italic EQUATE (01000H)
FONT:Underline EQUATE (02000H)
FONT:Strikeout EQUATE (04000H)
The tricky part here is the LONG is divided into 2 parts:
Upper half Style a USHORT Bitmap
Lower half Weight a USHORT Value
The Weight is Not a Bitmap it is a value from 100 to 999.
To get just the Weight use BAND(Style, FONT:Weight)
The Style Flags (Italic, Underline, etc) Are a Bitmap.
To get just those flags BAND(Style, BXOR(-1,FONT:Weight))
You can just check one flag easily e.g. IF BAND(Style, FONT:Italic)FONT:Italic THEN DO Its _Italic.
To get into all the ways to work with bits there are plenty of websites. Clarion works the same as all other languages.
That works as long as the bitmask have no overlap. When working with Bits the proper and safe way is to BOR() them. Clarion BOR() is limited to 2 operands where most languages allow many. You can make your own BORmany(LONG,LONG,<LONG>,<LONG>,<LONG>,<LONG>...,<LONG>)
Maybe you would find this useful for visualizing how bits are laid out in a hex digit. I guess I should have made it go right-to-left, but I kind of like it like this.
Basically, I think a good rule of thumb could be this:
If you are modifying existing values, you should ONLY use the appropriate bitwise functions.
If you trust that your bits won’t clash, and you are starting with no value (e.g., you are creating the mask value from scratch), and you don’t add the same equate twice, then it is OK (and even visually preferable) to add the values.
There could be more circumstances that might cause you to decide one way or the other.
IF we could use a “|” instead of “BOR()”, like in javascript, then BOR would be a lot more sexy than adding, but all of those parens get BORing.
Looking at my ~20 year old code is pretty difficult . It could definitely use some re-working. I didn’t have iWindowComponent on my radar at that time, and there are numerous other things I would have done differently if written today.