-2

I am trying to perform some custom operation but I noticed the following error. While calculating the value of t, I get desired output but for s I am getting negative values for no reason.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s = 'r';
    char temp1 = s;

    char t = 'a';
    char temp2 = t;

    s = (int(temp1) % int(96)) + (int(s) % int(96)) + 96;
    cout << int(s) << " ";

    t = (int(temp2) % int(96)) + (int(t) % int(96)) + 96;
    cout << int(t) << endl;
}

I have to use this logic elsewhere in a bigger program, I am getting the same error in both the cases

Output -124 98

I don't understand why -124 is begin printed

Rama
  • 3,222
  • 2
  • 11
  • 26
  • code style: don't say `int(96)`. Just say `96` – selbie Feb 06 '17 at 18:30
  • 2
    *I don't understand why -124 is begin printed* -- What did you expect it to print? Whatever your answer is, then consider what `char` means, since that is the variable you want to store the result in. – PaulMcKenzie Feb 06 '17 at 18:30
  • Did you perhaps want to use `unsigned char`? – stark Feb 06 '17 at 18:31
  • @selbie I am still getting the same error – Sudhanva Narayana Feb 06 '17 at 18:31
  • Of course you get the same error if you later cast the unsigned char to int which are signed – Sebastian Walla Feb 06 '17 at 18:33
  • @PaulMcKenzie it should print 132 – Sudhanva Narayana Feb 06 '17 at 18:33
  • 1
    @SudhanvaNarayana -- ok, so what is the range of a `signed char`? – PaulMcKenzie Feb 06 '17 at 18:33
  • @PaulMcKenzie -128 to 127? – Sudhanva Narayana Feb 06 '17 at 18:34
  • 1
    @SudhanvaNarayana So there is your answer. See how simple that was? 132 is not in the range of -128 to 127. Its bit pattern actually yields -124 if we're talking 2's complement. – PaulMcKenzie Feb 06 '17 at 18:35
  • @SudhanvaNarayana Just to fill you in -- here is the bit pattern for 132 -- `10000100`. Two's complement says to reverse the bits and add 1, So if you do that `01111011 + 1` = `01111100` == `124`. But since the most significant bit (the sign bit) of the original number was `1`, then the number becomes `-124`. You can also think of it as a circle. The highest number is 127, to go higher than that you get into the negative range, starting from `-128` and going down. So `132` is `127 + 5` = `-128, -127, -126, -125, -124`. – PaulMcKenzie Feb 06 '17 at 18:54

1 Answers1

1

You are hitting an overflow issue with an 8-bit integer type (char).

This expression is

s = (int(temp1) % int(96)) + (int(s) % int(96)) + 96;

Algebraically, your code simplifies to this:

s = 114 % 96 + 114 % 96 + 96;

s = 18 + 18 + 96;

s = (signed char)132;  // overflow!  132 won't fit in range [-128..127]

s = -124;

Change the declaration of s and t to be of type int. And some helpful ways to improve your code style are made as well:

int main()
{
    int s = 'r';
    int temp1 = s;

    int t = 'a';
    int temp2 = t;

    s = temp1 % 96 + s % 96 + 96;
    cout << s << " ";

    t = temp2 % 96 + t % 96 + 96;
    cout << t << endl;
}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • What if that char is a string? (Imagine I am looping through the string s[i]) – Sudhanva Narayana Feb 06 '17 at 18:41
  • You can cast the value back to `char` before printing it out. E.g. `cout << (char)t << endl`. But I have no idea what printing out "132" as a char value will render on the screen. – selbie Feb 06 '17 at 18:42
  • It is out of range assignment, not overflow. (Overflow means when the result of arithmetic operation is out of range of the type of the result type as deduced from the operands; but assignment is not an arithmetic operation). For signed integer types, overflow is usually undefined, whereas out-of-range assignment is implementation-defined. – M.M Feb 06 '17 at 20:10