Signed Extension

Jan 31, 2017 at 4:13am
Relatively new to c++.

Trying to figure out how to add zero's and one's to to the front of my signed binary string.

can anyone let me know where this code is going wrong? output is coming back as 0.



string signed_extension(string s) {
// you implement this one first
char arr[16] = "";
int len = 16 - strlen(arr);
memmove(arr + len, arr, strlen(arr));
for (int i = 0; i < len; i++)
if (s[0] = 0)
{
arr[i] = '0';
}
else
{
arr[i] = '1';
}
return "0";
}
Jan 31, 2017 at 4:27am
1
2
3
4
5
std::string signed_extension( const std::string& str ) {

    if( !str.empty() ) return str[0] + str ; // extend the sign bit
    else return "0" ;
}
Topic archived. No new replies allowed.