0
votes

I'm planning on creating a number class. The purpose is to hold any amount of numbers without worrying about getting too much (like with int, or long). But at the same time not USING too much. For example:

If I have data that only really needs 1-10, I don't need a int (4 bytes), a short(2 bytes) or even a char(1 byte). So why allocate so much? If i want to hold data that requires an extremely large amount (only integers in this scenario) like past the billions, I cannot. My goal is to create a number class that can handle this problem like strings do, sizing to fit the number. But before I begin, I was wondering..

bitset<1>, bitset is a template class that allows me to minipulate bits in C++, quite useful, but is it efficient?, bitset<1> would define 1 bit, but do I want to make an array of them? C++ can allocate a byte minimum, does bitset<1> allocate a byte and provide 1 bit OF that byte? if thats the case I'd rather create my number class with unsigned char*'s.

unsigned char, or BYTE holds 8 bytes, anything from 0 - 256 would only need one, more would require two, then 3, it would simply keep expanding when needed in byte intervals rather than bit intervals.

Which do you think is MORE efficient?, the bits would be if bitset actually allocated 1 bit, but I have a feeling that it isn't even possible. In fact, it may actually be more efficient to allocate in bytes until 4 bytes, (32 bits), on a 32 bit processor 32 bit allocation is most efficient thus I would use 4 bytes at a time from then on out.

Basically my question is, what are your thoughts? how should I go about this implementation, bitset<1>, or unsigned char (or BYTE)??

3
You can't allocate memory less than one char.Oliver Charlesworth
@OliCharlesworth Yes you can: char x[0];Pubby
@Pubby char x[0]; is invalid code; it's not allowed in c/c++BruceAdi
@Pubby lmfao, yes I thought so, then would bitset<1> be inefficient or are you unfamiliar with bitset ?u8sand
@u8sand bitset<1> will use at least one byte. In C++ objects cannot be smaller than one byte.R. Martinho Fernandes

3 Answers

2
votes

Optimizing for bits is silly unless you're target architecture is a DigiComp-1. Reading individual bits is always slower than reading ints - 4 bits isn't more efficient than 8.

Use unsigned char if you want to store it as a decimal number. This will be the most efficient.

Or, you could just use GMP.

1
votes

The bitset template requires a compile-time const integer for its template argument. This could be a drawback when you have to determine the max bits size at run-time. Another thing is that most of the compilers / libraries use unsigned int or unsigned long long to store the bits for faster memory access. If your application would run in a environment with limited memory, you should create a new class like bitset or use a different library.

1
votes

While it won't directly help you with arithmetic on giant numbers, if this kind of space-saving is your goal then you might find my Nstate library useful (boost license):

http://hostilefork.com/nstate/

For instance: if you have a value that can be between 0 and 2...then so long as you are going to be storing a bunch of these in an array you can exploit the "wasted" space for the unused 4th state (3) to pack more values. In that particular case, you can get 20 tristates in a 32-bit word instead of the 16 that you would get with 2-bits per tristate.