#include<stdio.h>
int main(void)
{
char c = 0x80;
printf("%d\n", c << 1);
return 0;
}
The output is -256
in this case. If I write c << 0
then the output is -128
.
I don't understand the logic behind this code.
Already your starting point is problematic:
char c = 0x80;
If (as seemingly in your case) char
is a signed type, you are assigning the integer constant 128
to a type that is only guaranteed to hold values up to 127
. Your compiler then may choose to give you some implementation defined value (-128
in your case I guess) or to issue a range error.
Then you are doing a left shift on that negative value. This gives undefined behavior. In total you have several implementation defined choices plus undefined behavior that determine the outcome:
char
128
to signed char
char
int
(there are three possibilities)int
It may be a good exercise for you to look up all these case an to see what the different outcomes may be.
In summary some recommendations:
char