English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

void fn(int n)
{
while(n != 0)
{
n >>= 1;
//do something
}
return;
}

2007-03-08 00:55:55 · 3 answers · asked by terrabytes404 2 in Computers & Internet Programming & Design

3 answers

To answer your question.

There is no output to the code shown.
There are no output statements and there is no return from the function.

Unless you want me to assume that the
//do something
is an output statement. I cannot because that is not your question.

2007-03-08 02:05:37 · answer #1 · answered by AnalProgrammer 7 · 0 0

Depending on the size of int the routine will execute the code in the while loop (after do something) x times so
if int is a 32 bit number because the MSB (most significant bit) is set to represent a negative number, the loop will execute 32 times.

e.g. int is an 8 bit number
n = 1xxxxxxx (where x is either 0 or 1 but doesn't matter)
n != 0 so execute loop and shift n right 1 bit, n= 01xxxxxx
n != 0 so execute loop and shift n right 1 bit, n= 001xxxxx
n != 0 so execute loop and shift n right 1 bit, n= 0001xxxx
n != 0 so execute loop and shift n right 1 bit, n= 00001xx
n != 0 so execute loop and shift n right 1 bit, n= 000001x
n != 0 so execute loop and shift n right 1 bit, n= 0000001
n != 0 so execute loop and shift n right 1 bit, n= 0000000
n == 0 DO NOT execute loop

2007-03-08 09:12:50 · answer #2 · answered by Tempest 3 · 2 0

if n<0 , the program control wont enter the while loop. The return statement will get executed by which control is transferred back to the calling function..
the result is same even if n>0.

2007-03-08 09:05:32 · answer #3 · answered by Afeef 2 · 0 2

fedest.com, questions and answers