Good afternoon,
This may sound like a silly question, but it would be really useful if there was a way around this… Is there any way I can get custom bit-depth integers (for example, a 20-bit integer) in C#?
Thank you very much.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Good question. As most things, the answer will depend on what you need to do with it.
I did something similar years ago maintaining a school research project. We had a custom integer type, though it was not necessarily defined by bit-depth per se. It was a prime-factor (with remainder) representation. This was very handy for what it was supposed to do, which was multiply and divide very large numbers. It was very poor for addition and subtraction.
There are also many other “custom” number implementations out there, a very common one is a custom Fixed Point representation (often seen when implementing deterministic physical simulations in games). There are probably some good overflow posts about this subject. These would be a good start for ideas on how to abstract and then implement a custom numeric representation.
If you are set on variable-bit-depth integer representation, consider wrapping an internal representation. One possibility may be to use an
IEnumerable<byte>and extend it byte by byte as its magnitude increases. Write operators that work against a dynamic range. This may not necessarily be the most optimal form of representation or most performant, but it would likely be the easiest.Another possible solution may be to do something similar, but with ‘ulong’. Not as space efficient, but we benefit from 64-bit operations.
Just spitballing 🙂
Hope this helps!