Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 byte unsigned integer c++

Tags:

c++

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?

like image 280
raze Avatar asked Apr 01 '12 17:04

raze


People also ask

What is the unsigned integer in C?

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);

What is the size of unsigned in C?

The size of both unsigned and signed char is 1 byte always, irrespective of what compiler we use.


1 Answers

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.

like image 168
Ben Voigt Avatar answered Oct 25 '22 13:10

Ben Voigt