C/C++ efficient bit array

前端 未结 10 848
清酒与你
清酒与你 2020-11-29 02:15

Can you recommend efficient/clean way to manipulate arbitrary length bit array?

Right now I am using regular int/char bitmask, but those are not very clean when arra

10条回答
  •  旧时难觅i
    2020-11-29 02:36

    I recently implemented a small header-only library called BitContainer just for this purpose. It focuses on expressiveness and compiletime abilities and can be found here: https://github.com/EddyXorb/BitContainer

    It is for sure not the classical way to look at bitarrays but can come in handy for strong-typing purposes and memory efficient representation of named properties.

    Example:

    constexpr Props props(Prop::isHigh(),Prop::isLow()); // intialize BitContainer of type Props with strong-type Prop
    
    constexpr bool result1 = props.contains(Prop::isTiny()) // false
    constexpr bool result2 = props.contains(Prop::isLow())  // true
    

提交回复
热议问题