Addition ASCII in c++ -


i change char variable.

using namespace std;      int main() {     char q;     q='b';     char c=127;     //cout << (int)q  << endl<< static_cast<int>('z');     char d= static_cast<char>(static_cast<int>((static_cast<int>('z') +19)));     cout << (int)d << endl;     system("pause"); } 

intupt:

-115 

why after addition variable assigned -115?

on platform, char 8 bit signed 2's complement type. overflowing type , behaviour of overflowing signed integral type undefined in c++. don't it.

notes:

  1. 8 bit mandated standard.

  2. char can unsigned or signed.

  3. if unsigned must have range [0, 255], , 255 + 1 0. overflow unsigned type is defined.

  4. if signed can either have range -128 +127 or -127 +127. latter range (1's complement) not allowed c++14. (interestingly that's not breaking change since difference in moving 1's 2's complement observable in malformed program such yours, if @ all!)

a simple remedy? use unsigned char.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -