4 bit container

Mar 15, 2011 at 12:06pm
Hello there gentelmen,
here is my problem. I was wondering. char is 1 byte, short is 2, int is 4 bytes etc. I have a need to calculate with a lots of very large numbers. Outside of the range of all of these containers. I therefore wanna use a lot of small containers to represent ints 0 to 9 I only need a 4bits to represent 16 different values so that would do me. Is there a way to map the memmory in to a 4bits containers or is the byte the smallest you can do on average pc
Cheers
Mar 15, 2011 at 12:11pm
closed account (z05DSL3A)
I have a need to calculate with a lots of very large numbers.

I would look into Arbitrary-precision arithmetic (AKA bignum arithmetic).

Something like: http://gmplib.org/
Mar 15, 2011 at 12:21pm
char is 1 byte, short is 2, int is 4 bytes etc

Right for char, at least in the C++ definition of a byte. Wrong for the others.

The smallest memory container your machine has is 1 byte.
Mar 15, 2011 at 1:18pm
If you're still after a container of bits, consider a bitset:
http://cplusplus.com/reference/stl/bitset/
Mar 15, 2011 at 1:27pm
If his goal was to save memory, that wouldn't really help him though.
Mar 15, 2011 at 1:32pm
Don't be so sure. It's portable, standard, and up to the implementation to do memory optimizations.

Unbuntu 10.10:
1
2
3
4
5
6
7
8
#include <iostream>
#include <bitset>

int main() {
    std::bitset< 8 * 100 > b;
    std::cout << sizeof( b ) << std::endl;
    return 0;
}





100
Last edited on Mar 15, 2011 at 1:41pm
Mar 17, 2011 at 12:46pm
Thank you guys for you advice
Topic archived. No new replies allowed.