# C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Point Out Correct Statements (Q.No. 3)

3.

Which of the following statements are correct about the program?

```
#include<stdio.h>
char *fun(unsigned int num, int base);
int main()
{
char *s;
s=fun(128, 2);
s=fun(128, 16);
printf("%s\n",s);
return 0;
}
char *fun(unsigned int num, int base)
{
static char buff[33];
char *ptr = &buff[sizeof(buff)-1];
*ptr = '\0';
do
{
*--ptr = "0123456789abcdef"[num %base];
num /=base;
}while(num!=0);
return ptr;
}
```

Discussion:

19 comments Page 1 of 2.
Manjushree said:
1 year ago

Give an explanation for the given program.

Newb said:
5 years ago

main() assigns s twice.

Last value, i.e. a hexadecimal representation of 128, will be printed. I believe this is graded wrong.

Last value, i.e. a hexadecimal representation of 128, will be printed. I believe this is graded wrong.

Mounika said:
6 years ago

Consider num=128 and base=2

in first iteration

*--ptr = "0123456789abcdef"[128 %2]; which is equals to "0123456789abcdef"[0];.

It means ptr stores 0th index of the string "0123456789abcdef"

num/=base; here num = 64.

Next iterations, num equals to 64,32,16,8,4,2 and for all these num values , 0th index of the string will be stored.

when num = 1 base =2.

*--ptr = "0123456789abcdef"[1 %2]; which is equals to "0123456789abcdef"[1];.

Now ptr stores 1st index of string "0123456789abcdef".

Finally, ptr contains 10000000 (due to *--ptr digits are stored from the right towards left)

(10000000)base2 = 128.

Similarly for 128,16 returns 80.

in first iteration

*--ptr = "0123456789abcdef"[128 %2]; which is equals to "0123456789abcdef"[0];.

It means ptr stores 0th index of the string "0123456789abcdef"

num/=base; here num = 64.

Next iterations, num equals to 64,32,16,8,4,2 and for all these num values , 0th index of the string will be stored.

when num = 1 base =2.

*--ptr = "0123456789abcdef"[1 %2]; which is equals to "0123456789abcdef"[1];.

Now ptr stores 1st index of string "0123456789abcdef".

Finally, ptr contains 10000000 (due to *--ptr digits are stored from the right towards left)

(10000000)base2 = 128.

Similarly for 128,16 returns 80.

Kalyan said:
6 years ago

I am not getting this, Can anyone explain it clearly?

Chandu said:
6 years ago

I don't understand this problem please explain clearly.

Emanuel said:
7 years ago

I don't think that it is possible to use "12345".

Zara said:
7 years ago

When we run this program the output is 80. Can one please explain how?

Machindra Mohate said:
8 years ago

Because base is 16 for hexadecimal. Its range starting from 0 to f or 0 to F.

To express it we use 0123456789abcdef.

To express it we use 0123456789abcdef.

Shubham said:
10 years ago

Understanding "0123456789abcdef"[num%base].

Think of 0123....ef as if char str[]="0123456789abcdef"

Now it becomes as:

str[num%base] .... I think rest is self explanatory!

Think of 0123....ef as if char str[]="0123456789abcdef"

Now it becomes as:

str[num%base] .... I think rest is self explanatory!

Ketan said:
10 years ago

Why specific size 33 of buffer?

Post your comments here:

Your comments will be displayed after verification.

Quick links

Quantitative Aptitude

Verbal (English)

Reasoning

Programming

Interview

Placement Papers

© IndiaBIX™ Technologies