Skip to content

function getKey() does not work with bitmasks with "holes" #18

@Tsaukpaetra

Description

@Tsaukpaetra

I was wondering why the getKey function didn't properly return flags when I realized it apparently expects every single value to be represented in the range of bits in the mask (in addition to a "zero" dummy value at the end of it all).

For instance, a bitmask that defines values of 1, 4, 128, you won't ever get a proper array for anything but the 1 value.

I propose that since each value is supposed to represent a single-bit mask in the bitmask anyways, the function could be thus:

    public function getKey()
    {
        $value = $this->value;
        $f     = array_filter(static::toArray(), function ($key) use (&$value) {
            return  $value & $key;
        });
        return array_keys($f);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions