hi,
with this code fragment:
#include <stdio.h>
void print_bit_string(char x)
{
int s = 128;
for (int i = 0;i < 8;i++)
{
printf("%d",x & s? 1: 0);
s >>= 1;
}
printf("\n");
}
char abs(char x)
{
if (x < 0)
{
x = -x;
}
return x;
}
int main()
{
char x = -1;
printf("x: %d abs: %d\n",x,abs(x));
print_bit_string(abs(x));
x = -128;
printf("x: %d abs: %d\n",x,abs(x));
print_bit_string(abs(x));
return 0;
}
why the output is as follows?
x: -1 abs: 1
00000001
x: -128 abs: -128
10000000