2

I can define values of an integer of different sizes in C++ using: std::int8_t, std::uint32_t, etc. Is there a way to define an integer that uses, say 14 bits?

oconnor0
  • 3,154
  • 6
  • 34
  • 52

3 Answers3

3

Is bitfield what you are looking for? In this example y takes 14 bits It's the closest match I can think of

struct x
{
    int y :14 ;
} ;
marom
  • 5,064
  • 10
  • 14
  • 1
    In C, a bit field of type `int` may be either signed or unsigned; it's implementation-defined. I don't know whether the same rule applies to C++. In any case, explicitly using `signed int` or `unsigned int` isn't a bad idea. – Keith Thompson Jun 22 '15 at 19:48
1

You can create a class that behaves exactly like std::uint14_t would behave if it existed. Since any values that don't fit with signed types invoke undefined behaviour, typedef std::int16_t int14_t is a perfectly fine.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

No. Read all the comments and other answers for clarification on why.

oconnor0
  • 3,154
  • 6
  • 34
  • 52