what is happening with my program output

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alireza6485
    New Member
    • Jan 2009
    • 19

    what is happening with my program output

    I run the following code

    Code:
    Code:
    int i;
    int j=2
    i=j+3
    int k=i/2
    k++
    k=i*j+1
    Console.Write.....
    Console.Write.....
    Console.Write.....
    i=j<<2
    Console.Write....
    int L=k+1
    j=j+1
    k=i*j+1
    Console.Write...
    Console.Write...
    Console.Write...
    Console.Write...
    and i get
    Code:
    i=5
    j=2
    k=11
    
    i=8
    
    i=8
    j=3
    k=25
    l=12
    Please let me know what is happening in this program,how do i get k=2 when k=i/2???
    where is i=8 coming from???
    Last edited by pbmods; Mar 5 '09, 12:15 AM. Reason: Added CODE tags.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    This line:
    i=j<<2
    It moves i up by two bit positions, so since j=2, then after this line i=8

    0000 0010 => 0000 1000

    [code=c#]
    int i;
    int j = 2;

    i = j + 3;//i=5
    int k = i / 2;//k=2
    k++;//k=3
    k = i * j + 1;//k=11
    Console.WriteLi ne("i=" + i.ToString());
    Console.WriteLi ne("j=" + j.ToString());
    Console.WriteLi ne("k=" + k.ToString());
    i = j << 2;//i=8
    Console.WriteLi ne("i=" + i.ToString());
    int L = k + 1;//L=12
    j = j + 1;//j=3
    k = i * j + 1;//k=25
    Console.WriteLi ne("i=" + i.ToString());
    Console.WriteLi ne("j=" + j.ToString());
    Console.WriteLi ne("k=" + k.ToString());
    Console.WriteLi ne("L=" + L.ToString());
    /*
    //and i get
    i = 5;
    j = 2;
    k = 11;

    i = 8;

    i = 8;
    j = 3;
    k = 25;
    l = 12;
    */
    [/code]

    Comment

    • snegulko
      New Member
      • Mar 2009
      • 5

      #3
      k = i / 2
      at that line u get k = 2 because k is defined as an int somewhere upper in your code and that means when u get a value that is not int by "/" it gets the number without the decimals. Converts it itself to int.

      Comment

      • kunal pawar
        Contributor
        • Oct 2007
        • 297

        #4
        You can debug code. Which help you what happen in code.

        Comment

        Working...