By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

what is happening with my program output

P: 19
I run the following code

Code:
Expand|Select|Wrap|Line Numbers
  1. int i;
  2. int j=2
  3. i=j+3
  4. int k=i/2
  5. k++
  6. k=i*j+1
  7. Console.Write.....
  8. Console.Write.....
  9. Console.Write.....
  10. i=j<<2
  11. Console.Write....
  12. int L=k+1
  13. j=j+1
  14. k=i*j+1
  15. Console.Write...
  16. Console.Write...
  17. Console.Write...
  18. Console.Write...
and i get
Expand|Select|Wrap|Line Numbers
  1. i=5
  2. j=2
  3. k=11
  4.  
  5. i=8
  6.  
  7. i=8
  8. j=3
  9. k=25
  10. l=12
  11.  
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???
Mar 4 '09 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
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

Expand|Select|Wrap|Line Numbers
  1. int i;
  2.             int j = 2;
  3.  
  4.             i = j + 3;//i=5
  5.             int k = i / 2;//k=2
  6.             k++;//k=3
  7.             k = i * j + 1;//k=11
  8.             Console.WriteLine("i=" + i.ToString());
  9.             Console.WriteLine("j=" + j.ToString());
  10.             Console.WriteLine("k=" + k.ToString());
  11.             i = j << 2;//i=8
  12.             Console.WriteLine("i=" + i.ToString());
  13.             int L = k + 1;//L=12
  14.             j = j + 1;//j=3
  15.             k = i * j + 1;//k=25
  16.             Console.WriteLine("i=" + i.ToString());
  17.             Console.WriteLine("j=" + j.ToString());
  18.             Console.WriteLine("k=" + k.ToString());
  19.             Console.WriteLine("L=" + L.ToString());
  20. /*
  21.             //and i get
  22.             i = 5;
  23.             j = 2;
  24.             k = 11;
  25.  
  26.             i = 8;
  27.  
  28.             i = 8;
  29.             j = 3;
  30.             k = 25;
  31.             l = 12;
  32. */
  33.  
Mar 4 '09 #2

P: 5
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.
Mar 5 '09 #3

kunal pawar
100+
P: 297
You can debug code. Which help you what happen in code.
Mar 5 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.