Home > Mobile >  Weird Data Types - C
Weird Data Types - C

Time:10-01

I know this sounds like a silly question, but I would like to know if it is possible in any way to make a custom variable size like this rather than using plain 8, 16, 32 and 64 bit integers:

uint15_t var; //as an example

I did some research online and I found nothing that works on all sizes (only stuff like 12 bit and 24 bit). (I was also wondering if bitfields would work with other data sizes too).

And yes, I know you could use a 16 bit integer to store a 15 bit value, but I just wanted to know if it is possible to do something like this.

How can I make and implement custom integer sizes?

CodePudding user response:

Inside a struct or class, can use the bitfields feature to declare integers of the size you want for a member-variable:

unsigned int var : 15;

... it won't be very CPU-efficient (since the compiler will have to generate bitwise-operations on most accesses to that variable) but it will give you the desired 15-bit behavior.

CodePudding user response:

You can try using bitfields, like many people mentioned. However, bitfields don't have a proper type. If you want to make your arbitrary-sized integers object-oriented, you can stuff the bitfield into a template:

template <int size> struct my_uint
{
    uint32_t value: size;
};
typedef my_uint<13> uint13_t; // some people use "using" syntax to do this
typedef my_uint<14> uint14_t;
typedef my_uint<15> uint15_t;

However, now you lost arithmetic operators, and you have to implement (overload) them yourself. You have to ask yourself many questions about what you really want to do with these new types:

  • Do you want to overload operators like , *, etc? Which ones?
  • Do you want to support arrays?
  • What is the maximum size you want to support? In my example, it's 32.
  • Do you want to support implicit constructors, e.g. uint15_t(uint32_t)?
  • How to support overflow?

There is no way to make your new types behave like built-in types - you can come close but cannot quite do it. That is, if you write a big program where you work with uint15_t and later you decide to switch to uint16_t, there will be subtle changes caused by uint16_t being a built-in type (e.g. consider rules about implicit conversions).

CodePudding user response:

To be able to use your bitfield int as a normal int but still get the behavior of a 15 bit int you can do it like this :

#include <cassert>
#include <utility>

template<typename type_t, std::size_t N>
struct bits_t final
{
    bits_t() = default;
    ~bits_t() = default;

    // explicitly implicit so it can convert automatically from underlying type
    bits_t(const type_t& value) :
        m_value{ value }
    {
    };

    // implicit conversion back to underlying type
    operator type_t ()
    {
        return m_value;
    }

private:
    type_t m_value : N;
};


int main()
{
    bits_t<int, 15> value;
    value = 16383;  // 0x3FFF
    assert(value == 16383);
    
    // overflow now at bit 15 :)
    value = 16384; // 0x4000, 16th bit is set.
    assert(value == -16384);

    return 0;
}

CodePudding user response:

bitfields feature will do the trick ...

uint32_t customInt : 15;
  • Related