~'q' << 6

This is an exercise from C++ primer on page 156.

'q' is 113 in int, and 01110001. ~'q' is then 10001110, and since it's promoted to int, it's 11111111 11111111 11111111 10001110. And finally it's shifted to left by 6, so it's 11111111 11111111 11100011 10000000.

 
cout << (int) 'q' << " " << ~'q' << " " << (~'q' << 6) << endl;


when I printed it, the output is

113 -114 -7296


my question is ~'q' should be promoted to int, and shouldn't the output be int and not signed char, which is represented by -114 in two's complement?

and for (~'q' << 6), I guess I am a bit rusty on how integer is represented, why is the value -7296?
my question is ~'q' should be promoted to int


It is being promoted to an int.

11111111 11111111 11111111 10001110 is the 32-bit representation of -114.

just like '10001110' is the 8-bit representation of -114.

and for (~'q' << 6), I guess I am a bit rusty on how integer is represented, why is the value -7296?


(~'q' << 6) is:

11111111 11111111 11100011 10000000, which is the 32-bit representation of -7296.

Each bit has a weight of 2n where 'n' is the bit index (bit 0 being the lowest bit, and bit 31 being the highest bit). You sum all the weights of the bits that are set (1), and that gets you the result.

With signed numbers, the high bit has a negative weight, which means its weight is actually -(2n).
since 11111111 11111111 11111111 10001110 is -114, that mean compiler knows it's a char and thus interpret that way instead of interpreting it as an int? and 000...000 10001110 is as well interpreted as char?
Why did you decide that the output -114 corresponds to signed char? If the compiler interprets -114 as an object of type signed char then instead of -114 the statement

std::cout << ~q;

would output some symbol instead of the value.

As for -7296 then SomeValue << 6 is equivalent to SomeValue * ( 2 ^6 ).
2^ 6 = 64. So -114 << 6 is equivalent to -114 * 64 and is equal to -7296.
Vlad is correct.

that mean compiler knows it's a char and thus interpret that way instead of interpreting it as an int?


No. The compiler is not interpretting it as a char. It's interpretting it as an int.

ints are perfectly capable of containing a value of -114. Case in point:

1
2
3
int an_int = -114;  // perfectly legal

cout << an_int;  // prints -114 


In binary... 11111111 11111111 11111111 10001110 is how an int represents the value of -114
Topic archived. No new replies allowed.