Answer the question
In order to leave comments, you need to log in
C#: What is A equal to?
A question from the code puzzles series. Perhaps it does not really fit into the topic of the Q&A section, but it falls short of the topic.
So, without compiling this code, can anyone tell what the value of variable A will be? int A = 1 << 32;
PS trick question =)
Answer the question
In order to leave comments, you need to log in
code puzzles
It will be interesting to know the answer (and explanation) if A <> 1 :) At least for those who do not know C#
According to the C# specification:
The high-order bits outside the range of the result type of x are discarded, the remaining bits are shifted left, and the low-order empty bit positions are set to zero.
It looks like the answer should be 0.
Not C#, but similar tricks in C++:
#include <cstdio>
#include <cmath>
int main() {
int i = 1, j = 32;
printf("%d %d %d %d\n", 1<<32, 1<<j, i<<32, i<<j);
}
[email protected] ~ % ./test
0 1 1 1
As someone who could not stand it and still compiled the code, I can say that the results
int A = 1 << 32;
are
different.
int A = 1;
A = A << 32;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question