# How to Store Multiple "Flags" in One LONG

**URL:** <https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642>\
**Category:** questions\
**Tags:** math, language\
**Created:** [November 4, 2024, 6:57pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642 "2024-11-04T18:57:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![donridley](https://clarionhub.com/user_avatar/clarionhub.com/donridley/32/1459_2.png) [@donridley](https://clarionhub.com/u/donridley)\
**Post date:** [November 4, 2024, 6:57pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/1 "2024-11-04T18:57:44Z")

</div>

Hello Everyone,

For some reason I am drawing a blank on this. It's something I haven't had to deal with for a long time.

Scenario:

Say I have multiple possible "flags" stored as Equates. For example:

```auto
FlagOne Equate(1)
FlagTwo Equate(2)
FlagThree Equate(3)
FlagFour Equate(4)
FlagFive Equate(5)
FlagSix Equate(6)

```

I have a TPS table that has a LONG to store flag options. Value could be one or more of the flag equates. For example:

Record One  
TpsLong = FlagOne

Record Two  
TpsLong = FlagFour + FlagFive + FlagSix

Record Three  
TpsLong = FlagTwo + FlagFour

Question:

When performing a loop through the records; How can I check if a record has one or more flags set?

I've tried several variations of Band(TpsLong,FlagOne) but I don't think I'm doing that correctly.

Thank you!!!!

Don

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 4, 2024, 7:06pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/2 "2024-11-04T19:06:24Z")

</div>

It’s easiest to think in hex for these bits.  
Each individual bit is either 1, 2, 4, or 8.

```clarion
MyBit0 EQUATE(01h)
MyBit1 EQUATE(02h)
MyBit2 EQUATE(04h)
MyBit3 EQUATE(08h)

```

then it moves to the next hex digit (nybble)

```clarion
MyBit4 EQUATE(10h)
MyBit5 EQUATE(20h)
MyBit6 EQUATE(40h)
MyBit7 EQUATE(80h)

```

Then continue all the way to (80000000h)

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 4, 2024, 7:19pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/3 "2024-11-04T19:19:09Z")

</div>

> [@donridley](#):
>
> I’ve tried several variations of Band(TpsLong,FlagOne) but I don’t think I’m doing that correctly.

Once you use the correct flags, this should work OK.  
Your existing flags are not correct. e.g. Flag3 is the same as combining Flag1 and Flag2.

You might find my bit pairs class to be useful in seeing how to set/read bits.

> **[GitHub - jslarve/BitPairsClass: JSBitPairsClass and sample program](https://github.com/jslarve/BitPairsClass)**
>
> JSBitPairsClass and sample program

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [November 4, 2024, 8:04pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/4 "2024-11-04T20:04:20Z")

</div>

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

```clarion
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:

1. Upper half Style a USHORT Bitmap
2. 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.

> [@donridley](#):
>
> Record Two  
> TpsLong = FlagFour + FlagFive + FlagSix

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>)`

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [November 4, 2024, 11:25pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/5 "2024-11-04T23:25:12Z")

</div>

> [@donridley](#):
>
> Say I have multiple possible “flags” stored as Equates. For example:
> 
> ```auto
> FlagOne Equate(1)
> FlagTwo Equate(2)
> FlagThree Equate(3)
> FlagFour Equate(4)
> FlagFive Equate(5)
> FlagSix Equate(6)
> 
> ```

As other have pointed out, your flags cannot overlap. So this could instead be:

```auto
FlagOne Equate(1)
FlagTwo Equate(2)
FlagThree Equate(4)
FlagFour Equate(8)
FlagFive Equate(16)
FlagSix Equate(32)

```

or you could represent them in binary or hex.

now say you want to set on FlagFour:

> TpsLong = bor(TpsLong, FlagFour)

and if you want to check if FlagFour is on:

> if band(TpsLong, FlagFour) then do something.

hth

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [November 4, 2024, 11:45pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/6 "2024-11-04T23:45:16Z")

</div>

> [@donridley](#):
>
> Record Three  
> TpsLong = FlagTwo + FlagFour

you can set on more than one flag at once, so instead of doing it in two statements like:

```auto
TpsLong = bor(TpsLong, FlagTwo)
TpsLong = bor(TpsLong, FlagFour)

```

you could simply say:

> TpsLong = bor(TpsLong, FlagTwo+FlagFour)

The difference in doing that compared to

> TpsLong = FlagTwo + FlagFour

is that using bor() will not upset/clear other existing flags that have already been set. I think this is probably why Carl said:

> When working with Bits the proper and safe way is to BOR() them

(unless Carl doesn’t like adding them within the bor() ?)

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [November 5, 2024, 1:37am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/7 "2024-11-05T01:37:21Z")

</div>

> [@vitesse](#):
>
> ` TpsLong = bor(TpsLong, FlagTwo+FlagFour)`

The most correct code is to not use Addition or Subtraction when doing Boolean.

Instead:  
` TpsLong = bor( bor(TpsLong, FlagTwo), FlagFour)`

Or:  
` TpsLong = bor(TpsLong,FlagTwo)`  
` TpsLong = bor(TpsLong,FlagFour)`

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 5, 2024, 2:07am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/8 "2024-11-05T02:07:55Z")

</div>

I agree completely, but sometimes it’s just easier to add the stuff up.

Like with FILEDIALOG(), for example. Nobody ever BOR’s those switches (FILE:Save+FILE:KeepDir+FILE:LongName, etc).

---

<div class="post-metadata">

**Author:** ![donridley](https://clarionhub.com/user_avatar/clarionhub.com/donridley/32/1459_2.png) [@donridley](https://clarionhub.com/u/donridley)\
**Post date:** [November 5, 2024, 12:58pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/9 "2024-11-05T12:58:48Z")

</div>

Stumbled across an interesting video explaining bitwise operators:

![](https://img.youtube.com/vi/BGeOwlIGRGI/maxresdefault.jpg)https://www.youtube.com/embed/BGeOwlIGRGI?feature=oembed&wmode=opaque

---

<div class="post-metadata">

**Author:** ![Flavio\_Suarez](https://clarionhub.com/user_avatar/clarionhub.com/flavio_suarez/32/1347_2.png) [@Flavio\_Suarez](https://clarionhub.com/u/Flavio_Suarez)\
**Post date:** [November 5, 2024, 1:46pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/10 "2024-11-05T13:46:02Z")

</div>

I use the BitList control template from ClarionMag… It has great features

 ![image](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/9/9120364c45c6fb304cd57b325b507a9446e0cce8.png)

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 5, 2024, 1:52pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/11 "2024-11-05T13:52:21Z")

</div>

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.

 ![Counting in Hex](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/2/2550d5f5bb03380999f25fb80e106264c07a647f.svg)

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [November 5, 2024, 7:48pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/12 "2024-11-05T19:48:29Z")

</div>

> [@jslarve](#):
>
> Like with FILEDIALOG(), for example. Nobody ever BOR’s those switches (FILE:Save+FILE:KeepDir+FILE:LongName, etc).

I agree. Another good example of Bit Maps is the Message Button Equates. They also can be added:

```clarion
BUTTON:OK EQUATE (01H)
BUTTON:YES EQUATE (02H)
BUTTON:NO EQUATE (04H)
BUTTON:ABORT EQUATE (08H)
BUTTON:RETRY EQUATE (10H)
BUTTON:IGNORE EQUATE (20H)
BUTTON:CANCEL EQUATE (40H)
BUTTON:HELP EQUATE (80H)

```

SVAPI.INC has many API examples.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 5, 2024, 8:12pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/13 "2024-11-05T20:12:29Z")

</div>

Basically, I think a good rule of thumb could be this:

1. If you are modifying existing values, you should ONLY use the appropriate bitwise functions.
2. 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.

e.g. [Here is a link to a javascript example](https://codepen.io/jslarve/pen/rNXZJrQ/3089d0aa02839cb84c4260e7c1e74d89?editors=0011)

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [November 5, 2024, 9:11pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/14 "2024-11-05T21:11:24Z")

</div>

> [@jslarve](#):
>
> 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.

Agree.

CW Utils should have a functions for And/Or/Xor that take more than 2. This should work…

```clarion
BOrMany PROCEDURE(LONG pValue, LONG pMask, |
                  <LONG p3>, <LONG p4>, <LONG p5>, <LONG p6>, <LONG p7>, <LONG p8>, <LONG p9>, |
                  <LONG p10>, <LONG p11>, <LONG p12>, <LONG p13>, <LONG p14>, <LONG p15>, <LONG p16>) !,LONG 
RV LONG,AUTO
    CODE
    RV=BOR(pValue,pMask)
    IF ~Omitted(p3) THEN RV=BOR(RV, p3).
    IF ~Omitted(p4) THEN RV=BOR(RV, p4).
    IF ~Omitted(p5) THEN RV=BOR(RV, p5).
    IF ~Omitted(p6) THEN RV=BOR(RV, p6).
    ...
    IF ~Omitted(p16) THEN RV=BOR(RV, p16).
    RETURN RV
```

---

<div class="post-metadata">

**Author:** ![elankreijer](https://clarionhub.com/user_avatar/clarionhub.com/elankreijer/32/1466_2.png) [@elankreijer](https://clarionhub.com/u/elankreijer)\
**Post date:** [November 11, 2024, 10:16am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/15 "2024-11-11T10:16:47Z")

</div>

> [@Flavio\_Suarez](#):
>
> I use the BitList control template from ClarionMag… It has great features

👍 I’ve also used BitList multiple times, it’s a simple but effective little class.

---

<div class="post-metadata">

**Author:** ![vitesse](https://clarionhub.com/user_avatar/clarionhub.com/vitesse/32/837_2.png) [@vitesse](https://clarionhub.com/u/vitesse)\
**Post date:** [November 11, 2024, 10:28am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/16 "2024-11-11T10:28:52Z")

</div>

> [@Flavio\_Suarez](#):
>
> I use the BitList control template from ClarionMag… It has great features

that was thanks to Jeff Slarve - see this earlier message:

> [@How do you display your bits?](https://clarionhub.com/t/how-do-you-display-your-bits/4990/3):
>
> maybe also have a look at Jeff’s bitlist class that was published in Clarion Magazine The Bitlist class and templates Jeff Slarve’s open source BitList template and class are a great way to manage large numbers of True/False values with a minimum of overhead. Posted Tuesday, March 07, 2000 ========================== BitList Template Update Jeff Slarve has released an updated version of his BitList template, which lets you store bit flags in LONG variables. The individual bits can be …

---

<div class="post-metadata">

**Author:** ![Joei](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/d78d45/32.png) [@Joei](https://clarionhub.com/u/Joei)\
**Post date:** [November 11, 2024, 11:07am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/17 "2024-11-11T11:07:32Z")

</div>

Not sure, but maybe this helps:

```clarion
GetBitInLong PROCEDURE (plBit,plLong) ! plBit = 0 ... 31. RETURN: 0, 1, 2, 4, ... 256
slMask LONG
  CODE
  slMask = 1
  slMask = BSHIFT(slMask,plBit)
  RETURN BAND(slMask,plLong)
! ---
SetBitInLong PROCEDURE (plBit,plLong) ! plBit = 0 ... 31
slMask LONG
  CODE
  slMask = 1
  slMask = BSHIFT(slMask,plBit)
  RETURN BOR(slMask,plLong)

```

---

<div class="post-metadata">

**Author:** ![Flavio\_Suarez](https://clarionhub.com/user_avatar/clarionhub.com/flavio_suarez/32/1347_2.png) [@Flavio\_Suarez](https://clarionhub.com/u/Flavio_Suarez)\
**Post date:** [November 11, 2024, 11:12am UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/18 "2024-11-11T11:12:37Z")

</div>

ClarionMag was always an invaluable source of information!

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [November 11, 2024, 2:45pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/19 "2024-11-11T14:45:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donridley](https://clarionhub.com/user_avatar/clarionhub.com/donridley/32/1459_2.png) [@donridley](https://clarionhub.com/u/donridley)\
**Post date:** [November 13, 2024, 1:05pm UTC](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642/20 "2024-11-13T13:05:50Z")

</div>

Hello Joei,

Thank you for sharing your code. Using the same concept, how would you clear a previously set BIT?

I have found an old suggestion from Bruce Johnson to use to “turn off” a BIT:

`x = BAND(x,255-SomeBit)`

```auto
GetBitInLong PROCEDURE (plBit,plLong) ! plBit = 0 ... 31. RETURN: 0, 1, 2, 4, ... 256
slMask LONG
  CODE
  slMask = 1
  slMask = BSHIFT(slMask,plBit)
  RETURN BAND(slMask,plLong)

SetBitInLong PROCEDURE (plBit,plLong) ! plBit = 0 ... 31
slMask LONG
  CODE
  slMask = 1
  slMask = BSHIFT(slMask,plBit)
  RETURN BOR(slMask,plLong)

```

[Next page](https://clarionhub.com/t/how-to-store-multiple-flags-in-one-long/7642.md?page=2)
