google / google/emboss

Built in function to count the number of bits set

Open
#98 3 comments 0 reactions 0 assignees View on GitHub
enhancement language feature
Dominant language
Python
Stars
92
Forks
30
PR merge metrics
No merged PRs in 30d

Description

Suppose you have a `bits` type like the following:

```
bits Features:
0 [+1] Flag foo
$next [+1] Flag bar
$next [+1] Flag baz
$next [+1] Flag abc
$next [+1] Flag xyz
```

A packet contains a bitset like the one above with the number of flags enabled determining the size of a variable length array.

```
struct Data:
0 [+1] Features features

let num_features = (features.foo ? 1 : 0) + (features.bar ? 1 : 0) + ...
$next [+num_features] UInt feature_values
```

The calculation of `num_features` can become quite a bit of toil. We have to physically write out the bits we wish to check. It would be easy to forget to add here if another needed to be checked in the future. It would be much easier to simply call out to a built in function which can do the counting for us.

Something like this would be so much better:

```
struct Data:
0 [+1] Features features

let num_features = bits_set(features)
$next [+num_features] UInt feature_values
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.