Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Bitflags

Tags:

I have several flags:

    None = 0
    HR = 1
    HD = 2,
    DT = 4,
    Fl = 8
..

etc

I want to create a function where I input a certain flag, lets say : 6.

Returned should be HDDT, since 2 | 4 = 6. It can also happen that 3 or more flags are combined or just a single one. e.g. : 7 = 1 | 2 | 4 => HRHDDT.

How can I return the concated string depending on the flag value?

In my case the enum has many more entries, which would make a simple if statement really uncomfortable, because I would have to cover hundreds of cases.

Is there any clever solution to this?

like image 980
Kyu96 Avatar asked May 22 '17 14:05

Kyu96


2 Answers

Let's assume we have a list somewhere with the powers of two:

flags = ['HR','HD','DT','Fl']

Then you can write the following function:

def power2():
    i = 1
    while True:
        yield i
        i *= 2

def obtain_flag(val):
    if val:
        return ''.join([f for f,i in zip(flags,power2()) if i&val])
    else:
        return None

The return None is not necessary: it is the flag that maps to zero.

Or if you want a list of the flags (in that case the flags can be anything):

def obtain_flag(val):
    if val:
        return [f for f,i in zip(flags,power2()) if i&val]

This gives:

>>> obtain_flag(6)
'HDDT'
>>> obtain_flag(7)
'HRHDDT'
>>> obtain_flag(0) # returns None, but None is not printed
>>> obtain_flag(1)
'HR'
>>> obtain_flag(2)
'HD'
>>> obtain_flag(3)
'HRHD'
>>> obtain_flag(4)
'DT'
like image 195
Willem Van Onsem Avatar answered Sep 25 '22 10:09

Willem Van Onsem


You can store the flags as a dict like so:

flags = {1:'HR', 2:'HD', 4:'DT', 8:'FL'}

and bit-wise and your number with the flags to retrieve the strings:

def get_flags(num):
    if num:
        return ''.join(flags[x] for x in flags if x & num)
    return None

>>> get_flags(6)
'HDDT'
like image 31
Billy Avatar answered Sep 24 '22 10:09

Billy