I have programmed a class called HugeInteger which can do arithmetic (add, sub, multiply) with numbers of "infinitely" size. It treats each bit of the digit in the number as a stand-alone digit (e.g. 1234 = 1, 2, 3 and 4). I store these numbers in a vector (vector<short>)
. Now, because each digit only can take the values from 0 to 9, i don't really need to store them as a 2 byte digit. Is there a way (without using char) to store the digits as a 1 byte unsigned integer? Thanks!
Update:
vector<unsigned char> v;
v.push_back(1);
v.push_back(2);
for (size_t i = 0; i < v.size(); i++)
cout << v[i];
This produces an unwanted output. Which datatype should I use to iterate through the vector?
An unsigned Integer means the variable can hold only a positive value. This format specifier is used within the printf() function for printing the unsigned integer variables. printf(“%u”, value);
The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use.
uint_least8_t
is the most compact data type for storing a single decimal digit.
If your system supports a data type of size 1 byte, this will be it. Otherwise it will be the next smallest data type available.
You may need to cast the value when using a stream insertion operator to make sure you get numeric output instead of character treatment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With