PopCount<T> and Generic Bit Counting for IBinaryInteger<T> #86545
-
I need to be able to get the number of bits as an integer needed for a generic integer data type (IBinaryInteger) inside a generic method. I can't find a property or method that gives me this directly, am I missing something? Assuming not, it seems like a great answer would be ...
... but the value returned from PopCount can't be cast to an integer. My next choice is something like ...
... which is mostly fine, except that it wouldn't work for a integer type that isn't a multiple of 8 (say a hypothetical UInt7). I'm left with counting the bits myself...
... which I don't mind (it works great), but I don't want to go down this path if there's a built-in way to do it. As an aside, what is the logic behind having PopCount return a T and not an int? It seems like an odd choice with limited use cases (particular since other PopCount methods, such as those in BitConverter, do return ints). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Use |
Beta Was this translation helpful? Give feedback.
Use
int.CreateXXX
to cast.