简体   繁体   中英

1 byte unsigned integer 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 (eg 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?

Yes, use unsigned char .

If <stdint.h> is available, then you could also use uint8_t .

Don't let the standard compiler type char confuse you; the following is perfectly legal:

char number[5] = { 1, 4, 3, 6, 2};   // Representation of decimal 14,362

It's not that there is anything special about the char type that forces you to think of them as characters; rather, it's is their convenient size of just 1 byte that makes them suitable to hold values that library routines such as printf use them to hold the 1-byte values that it will interpret as characters under a suitable encoding.

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.

The output you're seeing from using cout << on an unsigned char is down to the mechanics of the << operator when used with a std::ostream (specifically, different overloads of the operator << will display the value in different ways - the char/unsigned char overloads usually assume that you want a character representation instead of a numeric one)

The underlying representation of your unsigned char is still the same number which you pushed into the vector - an unsigned char is still an unsigned 1-byte integer)

If you wish to change the output, then you need to avoid using the overload of the << operator which is designed for char or unsigned char - the easiest way to do that is to perform a cast

vector<unsigned char> v;
v.push_back(1);
v.push_back(2);

for (size_t i = 0; i < v.size(); i++)
    cout << static_cast<int>( v[i] );

Using char or unsigned char as 1 byte integer type is not always that straightforward... Sometimes you just need the type to be number type, not character type. One such example is here: 1 byte integer data type Other is when you have function overloaded for arguments of several different types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM