The below function is intended to convert its parameter, an integer, from decimal to octal.
std::string dec_to_oct(int num) {
std::string output;
for(int i=10; i>=0; --i) {
output += std::to_string( (num >> i*3) & 0b111 );
}
return output;
}
It works for any positive input, however, for num = -1
it returns 77777777777
, when it should return 37777777777
, so the first digit needs to be a 3
instead of a 7
. Why is this happening? The function appears to be incorrect for all negative input. How can I adjust the algorithm so that it returns correctly for negative numbers?
Note: this is a CS assignment so I'd appreciate hints/tips.