Arbitrary Type Definition

N

Now With More Salt

Suppose I wanted to go about defining a three or four bit unsigned integer
type. How would I go about doing that? Thanks
 
B

Buster

Now With More Salt said:
Suppose I wanted to go about defining a three or four bit unsigned integer
type. How would I go about doing that? Thanks

You're going to have to provide a bit more motivation and explanation.
C++ doesn't have a 16 bit integral type, a 32 bit integral type or an 8 bit
integral type and gets along fine without them. What are you trying to do?

Regards,
Buster.
 
D

David B. Held

Now With More Salt said:
Suppose I wanted to go about defining a three or four bit
unsigned integer type. How would I go about doing that?

You could use a bit field embedded in a struct, but it would
not be treated as an equal with the builtins and would not
be packed optimally. C++ doesn't really let you specify the
size of types in that amount of detail. The resolution for type
sizes is sizeof(char). However, if you specify the operations
you want to support, and the expectations you have for the
type, you might be able to cobble together something that is
suitable for your purposes.

Dave
 
M

Mike Wahler

Now With More Salt said:
Suppose I wanted to go about defining a three or four bit unsigned integer
type. How would I go about doing that?

I'd create a class that wraps a std::bitset<num_of_bits>
and define whatever operations I need (e.g. arithmetic,
comparison with other types, etc.)

-Mike
 
T

Thomas Matthews

Now said:
Suppose I wanted to go about defining a three or four bit unsigned integer
type. How would I go about doing that? Thanks

One method is to use an existing integral type that is bigger
than what you need, and perhaps typedef it.

For example, if your platform's native integral type uses
64 bits, then you would only use the "lower" 3 or 4 bits.

But if your processor has a native integral type that
uses 3 or 4 bits, then I suggest assembly language.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top