Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?    

void main()
    {
        int x = 1, z = 3;
        int y = x << 3;
        printf(" %d\n", y);
    }

 

  • A.-2147483648
  • B.-1
  • C.Run time error
  • D.8

Answer: D

Correct answer: 8

<< shifts the bits of x left by z places, and each place doubles the value:

\[ 1 \ll 3 = 1 \times 2^3 = 8 \]

So y is 8. Shifting left by \(n\) is multiplication by \(2^n\), which is why compilers turn such multiplications into shifts.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.