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;
};
Share:
16,671
Aman Gupta
Author by

Aman Gupta

Updated on June 22, 2022

Comments

  • Aman Gupta
    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
    Aman Gupta almost 7 years
    But 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
    krzaq almost 7 years
    It would be true if you used uint8_t or char instead (assuming 8-bit char)
  • Aman Gupta
    Aman Gupta almost 7 years
    nibble is also used only in the struct. Is there any direct way to use the 4 bit int
  • Richard Critten
    Richard Critten almost 7 years
    Note 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.