Can create a 10 bit data type? I can use uint16_t
, uint32_t
, and so forth, but what if I want to use uint10_t
or uint12_t
? Is this possible?
Asked
Active
Viewed 8,866 times
3
-
1The beauty of C++ is that with a little bit of elbow grease you can define any datatype your heart desires. – Sam Varshavchik May 27 '16 at 01:49
-
1Hmm, what about a `std::bitset<10>`? – πάντα ῥεῖ May 27 '16 at 01:51
-
How would you want it to behave? How would you want it to be different than a uint_16? – Andy Schweig May 27 '16 at 01:52
-
4You can't because there is no C/C++ language and the solution will be dramatically different (*although a [tag:c] solution might work in [tag:c++]*). – Iharob Al Asimi May 27 '16 at 01:55
-
1[10 or 12 bit field data type in C++](http://stackoverflow.com/q/29529979/995714), [2 bits size variable](http://stackoverflow.com/q/14600153/995714), [Which C datatype can represent a 40-bit binary number?](http://stackoverflow.com/q/9595225/995714) – phuclv May 28 '16 at 03:43
1 Answers
6
You can't have a type whose size isn't a multiple of bytes - so creating a 10-bit datatype is a nonstarter.
You can create a type which represents entities that contain 10 bits though: whether using std::bitset<10>
or using a 10-bit bitfield in a 16-bit type:
struct uint10_t {
uint16_t value : 10;
uint16_t _ : 6;
};
In both cases, the type itself will be larger than 10 bits. So you wouldn't be able to create an array of 8 of them to fit into 10 bytes, for instance.

Barry
- 286,269
- 29
- 621
- 977
-
3IIRC, the unused bitfield member doesn't have to be named. (Actually, it wouldn't even need to be included) – owacoder May 27 '16 at 02:16
-