Looking for int/bitmap logic

I’m looking for a clever way to “map” three int-codes and their possible combinations into one field.

Think of something like colors:
red = 1
green = 2
blue = 3

Every color can be picked. I don’t want to use three boolean flags, since I expect large volumes (I want to log this as events)

I can’t just add or multiply the values, because 1 + 2 = 3 (lol obvious) so I wouldn’t know if it was red and green or just blue - hope you know what I mean :wink:

It’s not really a GO question, rather general logics. I also want to avoid int/string conversion to “build” a new number like “123” or “12” for performance reasons.

I was thinking of bitmaps/bit-logic or something…not sure - any thoughts?

1 Like

Okay, didn’t know what to search for, it’s “bitwise” and &

didn’t think of & as it’s the pointer/address operator :wink:

Hello there!

You can use uint8 as an example to store 11000000 as red, 00110000 as blue, as 00001100 as green. And go on with red+blue intersection with bitwise OR, blue+green intersection, green+red intersection etc… but if you really want to get all of RGBA(0,0,0,0) - RGBA(255,255,255,255) range you will need 8 X 4 bits which is 4 bytes for all RGBA values. In that case, you would be better of with hexadecimal values inside uint32. Hope this makes sense.

Some bitwise operations are found here:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.