Why does 'std::cout << !+2 ' output 0?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why does 'std::cout << !+2 ' output 0?



This line of code outputs 0:


0


std::cout << !+2;



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


35


33


2


35



Why is it like that?





std::cout << ('!' + 2)? 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 6 at 2:58



std::cout << ('!' + 2)


+2


false


0





no , std::cout<<!+2;
– Subodh
Aug 6 at 3: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 6 at 3:00



!+2





@HenriMenke correct thanks
– Subodh
Aug 6 at 3:05




2 Answers
2



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.


!+2


+2


0


false


true


!+2


!true


true


false


!+2


false


std::cout


true


1


false


0


std::cout << !+2;


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.


!


std::cout << ('!' + 2);


#include <iostream>

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



Output:


35



Live on Wandbox





"+2" +sign also going to convert to binary if yes in case of negative sign -0 it should return 1
– Subodh
Aug 6 at 3:13





@Subodh There is no such things as -0 in integers.
– Henri Menke
Aug 6 at 3:15


-0





@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
– Scheff
Aug 6 at 8:32



float


double


-0.0


+0.0


int


-0


0


!-0


!0


1





the unary plus is not for converting to binary but for promoting the type to int
– phuclv
Aug 7 at 5:37


int



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;





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 6 at 11:51


'!'





Actually, since it is a literal, the compiler would emit it using the target character encoding (-fexec-charset or /execution-charset), which is very unlikely to be ASCII (but very likely to be equivalent to ASCII for the common characters).
– Tom Blodget
Aug 7 at 16:23






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.