Is it possible to create my own data types? I want to create a datatype like int but with 3bytes(24 bits) instead of 4 or 2.
Thanx in advance!
Is it possible to create my own data types? I want to create a datatype like int but with 3bytes(24 bits) instead of 4 or 2.
Thanx in advance!
In short, no. In long, kind of.Originally posted by Benzakhar
Is it possible to create my own data types? I want to create a datatype like int but with 3bytes(24 bits) instead of 4 or 2.
Thanx in advance!
There is no direct way for you to do it in c++ without having to be at least a little nonstandard. The best you can do is allocate 24 bits in a non-padded class (thats where the nonstandard functionality is) then overload all operators for it so that is works exactly like a built-in datatype by taking the data and interpretting it into an int (another possible place where you'd have to make nonstandard assumptions). You can use boost/operators.hpp to make things easier on yourself.
In the end you're best off just dealing with the fact that you're gonna waste some space. It's much faster (both for you while programming, and for your program at runtime).
Another alternative is to be creative with bitfields for whatever type you need the space for. Even then you most-likely won't be able to do it (IE, if your structure needs a 24 bit unsigned int and an 8 bit char, you can use bitfields to get a 32-bit structure, but you won't be able to do the same with 2 24-bit unsigned ints and expect a 48-bit structure even if you have padding off).
Last edited by Polymorphic OOP; 12-29-2003 at 12:41 PM.
The main problem is how you are going to protect the byte immediately following the data structure, since there is no 3 byte copy instruction?
If the data encroaches/overlaps with higher memory addresses during a copy operation for instance, you could have a real corruption problem on you hands.
One solution would be to string together some half adder gates, carries connected and do the math just like the cpu itself does - bit by bit. That way you could even expand your capacity far beyond a mere 24 bits - like 256 bit ints made up of 32 bytes - or an extremely huge int, composed of thousands of bits for that matter (I used a similar technique with a large int class I made a while back).
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }