create 24bit int

This is a discussion on create 24bit int within the C++ Programming forums, part of the General Programming Boards category; Is it possible to create my own data types? I want to create a datatype like int but with 3bytes(24 ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    create 24bit int

    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!

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: create 24bit int

    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!
    In short, no. In long, kind of.

    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 11:41 AM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21