Is there an actual 4-bit integer data type in C++11
16,671
Solution 1
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
Solution 2
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};
Author by
Aman Gupta
Updated on June 22, 2022Comments
-
Aman Gupta almost 2 years
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
-
Aman Gupta almost 7 yearsBut the struct also use memory space ,so how much the total space is used in this case.
-
πάντα ῥεῖ almost 7 years@emlai That's unfortunately not true.
-
krzaq almost 7 yearsIt would be true if you used
uint8_t
orchar
instead (assuming 8-bit char) -
Aman Gupta almost 7 yearsnibble is also used only in the struct. Is there any direct way to use the 4 bit int
-
Richard Critten almost 7 yearsNote that
sizeof(char)
always returns 1, so nothing can be smaller than a char - even is char is not 8 bits wide. -
πάντα ῥεῖ almost 7 years@AmanGupta " Is there any direct way to use the 4 bit int" No.