C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
Discussion:
70 comments Page 5 of 7.
Razia said:
1 decade ago
Why it is printing only mask value?
Razia said:
1 decade ago
It is supposed to print c|mask, instead it is printing only the value of mask.
Pritam said:
1 decade ago
At 5th loop mask=01000 and 48 or 01000 gives 64 then how it comes 48 again?
Jayadeep said:
1 decade ago
Hi @arun
Here c is assigned 48 so its binary value is 110000
mask is assigned 1 i.e 00000001
So while running the for loop first time
c|mask means bitwise operation is performed so each bit is manupulated
So c|mask gives
00110000
00000001
--------
00110001 = 49
But we are printing using %c so the ascii character corresponding to this is printed
So 1 is printed
then the mask is left shifted so it becomes 00000010
so if we perform or operation we will get 50 which is equal to 2
like this it prints 12345
Here c is assigned 48 so its binary value is 110000
mask is assigned 1 i.e 00000001
So while running the for loop first time
c|mask means bitwise operation is performed so each bit is manupulated
So c|mask gives
00110000
00000001
--------
00110001 = 49
But we are printing using %c so the ascii character corresponding to this is printed
So 1 is printed
then the mask is left shifted so it becomes 00000010
so if we perform or operation we will get 50 which is equal to 2
like this it prints 12345
Arun said:
1 decade ago
#include<stdio.h>
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
Please explain above the program.
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}
Please explain above the program.
Bhavana said:
1 decade ago
Well said. For clearing my confusion.
Yamini said:
1 decade ago
I cant understand that left shift operation "<<"
Can anyone pls explain me n make me clear
Can anyone pls explain me n make me clear
Sowmya said:
1 decade ago
Thanks a lot Sai!! :) :)
Sneha said:
1 decade ago
Thanks sai.
Samatha said:
1 decade ago
Well. Thanku.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers