Bit mask to represent set of flags

5,400 views
Skip to first unread message

bsr

unread,
Feb 17, 2014, 4:29:06 PM2/17/14
to golan...@googlegroups.com
Hello,

I hate to ask this question without a "play" link, but I am not sure where to begin in terms of data type to use. I am in need of some flags, which currently are represented as bool. Since these would be part of many data points, to save space, I was thinking to use bit representation and masking them to get the flag status. For say, 8 flags, can you please show an example where I can set these bits and read them back. 

Thanks
bsr

egon

unread,
Feb 17, 2014, 4:42:45 PM2/17/14
to golan...@googlegroups.com

Caleb Spare

unread,
Feb 17, 2014, 4:44:36 PM2/17/14
to bsr, golang-nuts
This is commonly used in the standard library. A really simple example (only two flags) is




--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

bsr

unread,
Feb 17, 2014, 4:53:22 PM2/17/14
to golan...@googlegroups.com
thank you verymuch. One question.

if flags&Alpha != 0 { 

 is it an int equality check ? I am sure it is valid, as it works. but, the results of bitwise and, or etc would be int 1 / 0 ?

egon

unread,
Feb 17, 2014, 5:08:02 PM2/17/14
to golan...@googlegroups.com


On Monday, February 17, 2014 11:53:22 PM UTC+2, bsr wrote:
thank you verymuch. One question.

if flags&Alpha != 0 { 

 is it an int equality check ? I am sure it is valid, as it works. but, the results of bitwise and, or etc would be int 1 / 0 ?

Don't guess such things... try them and you'll see what it produces... r := flagsΒ fmt.Printf("%v %t\n", r, r) 

bsr

unread,
Feb 17, 2014, 5:17:48 PM2/17/14
to golan...@googlegroups.com
thank you.  played around with the printf and learned something :-)

wilbert.van....@gmail.com

unread,
Oct 22, 2014, 4:35:20 PM10/22/14
to golan...@googlegroups.com
In the link below you use the following:

// remove Alpha
flags ^= Alpha

This is a bitwise XOR operation and will basically flip Alpha. If it is set it becomes unset and vice versa. When you repeat the instruction:

// remove Alpha
flags ^= Alpha

if flags&Alpha == 0 {
fmt.Println("Alpha is not set")
}

// remove Alpha
flags ^= Alpha

if flags&Alpha == 0 {
fmt.Println("Alpha is not set")
}

it will output "Alpha is not set" only once, because the second time, Alpha is set again.

To truly remove Alpha use

// remove Alpha
flags &^= Alpha

this is the AND NOT operator and will clear the Alpha flag from flags whether it is set or unset.

Go Happy!

Wilbert

Tahir

unread,
Oct 22, 2014, 6:12:19 PM10/22/14
to golan...@googlegroups.com
Alternatively, this is quite the ugly code so I'm a little bit reluctant to put it here but just so that it gives you ideas :
It's for a bitmap of 64 too... but could easily be translated to uint8.

Tahir

unread,
Oct 22, 2014, 6:30:21 PM10/22/14
to golan...@googlegroups.com
Oh Wilbert... You made me respond to a buried thread .. grrr
Well at least it is not 4 years old.

wilbert.van....@gmail.com

unread,
Oct 22, 2014, 9:04:43 PM10/22/14
to golan...@googlegroups.com
I was playing around with Go and stumbled upon this. I noticed the risky code and thought someone might stumble upon it too. I noticed the timestamp, but added the comment anyway for keepsake.

Your code looks interesting enough to study and figure out what it is doing....... Never too late to learn.

Thanks.

Wilbert

Tahir

unread,
Oct 23, 2014, 5:31:51 AM10/23/14
to golan...@googlegroups.com
It's ok, no worries ... I wasn't being serious :P


On Monday, February 17, 2014 9:29:06 PM UTC, bsr wrote:

Albulescu Cosmin

unread,
Mar 3, 2017, 4:02:51 PM3/3/17
to golang-nuts
Its easy to use bit flag operations. But more easy i think is to use a small library for this, a library like this: https://github.com/albulescu/go-bitflag
Reply all
Reply to author
Forward
0 new messages