C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 5)
5.
What will be the output of the program?
#include<stdio.h>

int main()
{
    unsigned char i = 0x80;
    printf("%d\n", i<<1);
    return 0;
}
0
256
100
80
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
65 comments Page 3 of 7.

Sushma said:   1 decade ago
I've the same doubt..:(

char is 1 byte
so, 0x80= 1000 0000
i<<1 = 0000 0000 rite?

Is it dat %d in the printf statement matters?

Ravikant said:   1 decade ago
Bitwise operators operate on always nos. Means int, so it converted into int.

Mehul said:   1 decade ago
@Apurva Nigam Thanks a lot.

Sutendra mirajkar said:   1 decade ago
#include<stdio.h>

int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d %d %d\n", i);
return 0;
}

Then the output of i is zero can anyone explain why is it so?

Chotu said:   1 decade ago
Dear sutendra Mirajkar

int main()
{
unsigned char i = 0x80;
i=i<<1;// i is char so 0.
0x80 in binary 1000 0000
after letf shift(i<<1) is 0000 0000 .That value store in i char(1byte 00000000)
printf("%d\n", i);// out put is 0
return 0;
}
//////////////////////////////////////

our result in inter thats y i take int var; i<<1 value is store int var. The output is 256
int main()
{
unsigned char i = 0x80;
int var;
var=i<<1;
printf("%d %d %d\n", var);
return 0;
}

Sumit said:   1 decade ago
Nikita its not << operator , which converts char to int , its %d which makes changes with c , try to use %c it will give blank as , Ascii value of NULL is 0

Spa said:   1 decade ago
Why everyone taking for char 2bytes. ? can any one explain me.

Kuljeet said:   1 decade ago
@Spa- because its an unsigned char value. its range is 0 to 255. hence it can take any value between 0 and 255, which in this case is 80, represented in hexadecimal form.

Chandini said:   1 decade ago
Please explain briefly about this program.

ANJANI said:   1 decade ago
Thanks a lot. For your nice explanation. I m fully overwhelm.


Post your comments here:

Your comments will be displayed after verification.