L
L
lam0x862011-03-26 03:43:09
.NET
lam0x86, 2011-03-26 03:43:09

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

9 answer(s)
X
xRay, 2011-03-26
@lam0x86

code puzzles

Yes, and initially in Java http://stackoverflow.com/questions/702607/in-java-when-using-bitshifts-why-does-1-32-1-31-1

S
sl_bug, 2011-03-26
@sl_bug

It will be interesting to know the answer (and explanation) if A <> 1 :) At least for those who do not know C#

N
Nurzhan Bakibaev, 2011-03-26
@Hypuk

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.

X
xappymah, 2011-03-26
@xappymah

If A != 0 here, then I'd rather not learn C# :)

B
burdakovd, 2011-03-26
@burdakovd

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

A
Alexey Grichenko, 2011-03-26
@Kalobok

I vote for 1. :)

S
S_talker, 2011-03-26
@S_talker

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;

M
Monca, 2011-03-26
@Monca

I don't know C#, but the answer should be 0

S
sl_bug, 2011-03-26
@sl_bug

Checked on Mono for the sake of interest. Still 1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question