0

What is the int value of 456789*456789? (regular 32-bit representation)

edit - sharping the question: what is the 4 bytes representation of the value?

1) -1,797,206,983

2) 206,656,190,521

3) 2,497,760,313

4) 350,276,665

5) No value: the code terminated with error message.


I know the answer is 1. I wonder how one can answer this question without calculator or any other utilities (like in a test)

Ygrno
  • 141
  • 1
  • 8
  • That depends on the language, `int` has different meanings depending on the language . – Hatted Rooster Jun 26 '18 at 10:02
  • The exact behavior depends, if you try that expression in C# it would actually not compile because it overflow int, and since you haven't explicitly stated that you want long (64-bit) it won't even compile the expression for you. – Lasse V. Karlsen Jun 26 '18 at 11:12

2 Answers2

1

My approach when reading your question:

  • the answer is going to be larger than 400000 * 400000, which is 16 and ten zeroes (this calculation I can do in my head), so answers 3 and 4 can be excluded.
  • I know (simply because I just know a couple of common powers of two) that a 32bit value can hold something like 4.2 billion values, so answer 2 can be excluded too because the value is too large to fit into 32bit

So I'd go for answer 1) (without knowing whether it's correct).

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
  • That would be my approach too , though I wonder if there's another way to like.. know its gonna be negative or something like that – Ygrno Jun 26 '18 at 11:15
0

It depends on how the specific language manages overflow in data type operations, e.g.

  • Java (reference), C and C++ go back to the minimum value and continue from there, returning -1797206983;
  • C# returns the compile error "The operation overflows at compile time in checked mode";
  • PHP and JavaScript (example below) return 208656190521.

console.log(456789 * 456789);
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
  • I guess this would be the most accurate answer, but if we assume it's java or like what happens when I use windows 10 calculator on programmer mode how would you answer this question on paper? you have 4 bytes to represent the result how would you know its option 1? – Ygrno Jun 26 '18 at 10:17