Fix ubsan runtime error: left shift of negative value -9
This commit is contained in:
parent
73e365d398
commit
1eaf9f3269
@ -33,20 +33,16 @@ std::string encode(std::vector<int> &numbers)
|
|||||||
std::string output;
|
std::string output;
|
||||||
for (auto &number : numbers)
|
for (auto &number : numbers)
|
||||||
{
|
{
|
||||||
bool isNegative = number < 0;
|
if (number < 0)
|
||||||
|
|
||||||
if (isNegative)
|
|
||||||
{
|
{
|
||||||
const unsigned binary = std::llabs(number);
|
const unsigned binary = std::llabs(number);
|
||||||
const unsigned twos = (~binary) + 1u;
|
const unsigned twos = (~binary) + 1u;
|
||||||
number = twos;
|
const unsigned shl = twos << 1u;
|
||||||
|
number = static_cast<int>(~shl);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
number <<= 1u;
|
|
||||||
|
|
||||||
if (isNegative)
|
|
||||||
{
|
{
|
||||||
number = ~number;
|
number <<= 1u;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const int number : numbers)
|
for (const int number : numbers)
|
||||||
|
Loading…
Reference in New Issue
Block a user