0

The following code is part of a larger class that I am trying to debug.

int val_a = 'a'; // 'a' == 97 (ASCII code)
int answer = 1 << val_a;

The value of answer is 2 when I ran the code through my Java compiler.

So if I'm doing left shifting correctly (which I looked at several online tutorials), it will be 1 * 2^97 which is 1.5845632.... I tried 'b' (ascii code is 98) 1 * 2^98 which is 3.16912650... and the value of the answer is 4 and so forth with 'c', 'd' ...

I'm not sure how to arrive at those answers.

Thanks in advance!

Allen
  • 181
  • 1
  • 4
  • 16
  • 1
    See https://stackoverflow.com/questions/34193787/weird-result-of-java-integer-left-shift. 97 % 32 is 1, so its actually shifted by 1 – MDK Oct 16 '20 at 22:21
  • @Allen - it's unclear what you expected to happen. In Java you can't shift an int by 97 places (as explained by the previous comment), but if you **could**, the result of left-shifting **any** 32-bit int value by 97 places would always be zero. An int cannot hold an arbitrarily large value. – user14387228 Oct 17 '20 at 01:58

0 Answers0