-2

This line of code outputs 0:

std::cout << !+2;

I think it should be 35, since '!' has an ASCII code of 33 and adding 2 to it equals 35.

Why is it like that?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Subodh
  • 40
  • 5
  • 1
    [`std::cout << ('!' + 2)`](https://wandbox.org/permlink/aat6nZdIH7VFiI00)? See how I quoted the exclamation mark? The bare exclamation mark is the NOT operator which will negate the truth value of `+2` which is `false` which is `0` when converted to integer. – Henri Menke Aug 06 '18 at 02:58
  • no , std::cout<<!+2; – Subodh Aug 06 '18 at 03:00
  • First thing you need to do is figure out what the expression `!+2` does. Hint--it doesn't do what you think it does. – rsjaffe Aug 06 '18 at 03:00
  • @HenriMenke correct thanks – Subodh Aug 06 '18 at 03:05

2 Answers2

6

Let's quickly analyze what your code !+2 does. The bare exclamation mark is the logical not operator which negates the truth value of its operand. Integers, such as +2 can be converted to boolean, where 0 means false and every non-zero integer true. That means that !+2 is converted to !true. The negation of true is obviously false, so !+2 is converted to false. When you pipe this boolean into std::cout is is converted to integer again, i.e. true turns into 1 and false turns into 0. That is why you std::cout << !+2; prints 0.

What you wanted to do instead (add 2 to the ASCII code of !) can be achieved as well. Therefore you have to tell the compiler that you want the character ! by enclosing it in single quotes. Then the code std::cout << ('!' + 2); will print 35, as expected. I added some extra parentheses to not rely purely on operator precedence.

#include <iostream>

int main() {
    std::cout << ('!' + 2) << '\n';
}

Output:

35

Live on Wandbox

Henri Menke
  • 10,705
  • 1
  • 24
  • 42
  • "+2" +sign also going to convert to binary if yes in case of negative sign -0 it should return 1 – Subodh Aug 06 '18 at 03:13
  • 1
    @Subodh There is no such things as `-0` in integers. – Henri Menke Aug 06 '18 at 03:15
  • @Subodh Floating point numbers (like e.g. `float` and `double`) are able to encode `-0.0` vs. `+0.0` but integral types (like e.g. `int`) cannot. Though, you may write `-0` but the result is `0`. Consequently, `!-0` == `!0` == `1`. [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/8e9bcc11fce9f3d4) – Scheff's Cat Aug 06 '18 at 08:32
  • the unary plus is **not** for converting to binary but for [promoting the type to `int`](https://stackoverflow.com/q/14365732/995714) – phuclv Aug 07 '18 at 05:37
0

If you want to get the ASCII value of exclamation mark, you need to surround it with single quotes like following.

std::cout << '!' + 0;

What you did is negating a value (this value can be either True or False). Making the value (here integer) positive or negative does not matter (here you explicitly specify 2 as positive), because everything other than zero means True. So, if you do the same thing for zero like following, you would get 1 as output.

std::cout << !+0;
eneski
  • 1,575
  • 17
  • 40
  • 1
    Note that `'!'` is a character constant; its value is determined by the character encoding that's in use. These days that's almost always ASCII, but it's not required to be ASCII, and there are systems where it isn't. – Pete Becker Aug 06 '18 at 11:51
  • 1
    Actually, since it is a literal, the compiler would emit it using the target character encoding ([-fexec-charset](https://gcc.gnu.org/onlinedocs/cpp/Invocation.html#index-fexec-charset) or [/execution-charset](https://learn.microsoft.com/en-us/cpp/build/reference/execution-charset-set-execution-character-set)), which is very unlikely to be ASCII (but very likely to be equivalent to ASCII for the common characters). – Tom Blodget Aug 07 '18 at 16:23