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 6 of 7.
Lavanya duddu said:
1 decade ago
<< means Leftshoft operations
lets take number 2 (0010) if you do left shift operation it becomes 0100(4)
so short cut for << operation is just multiply by two.
lets take number 2 (0010) if you do left shift operation it becomes 0100(4)
so short cut for << operation is just multiply by two.
Bhavana said:
1 decade ago
Well said. For clearing my confusion.
Poobal said:
1 decade ago
Please any one can explan this program clearly ?
Adi said:
1 decade ago
Razia, its not printing mask.
Its printing 49 as 49-48=1
then 50-48=2
then 52-48=4 and
then 48-48=0 on it prints 12480.
Its printing 49 as 49-48=1
then 50-48=2
then 52-48=4 and
then 48-48=0 on it prints 12480.
Pawan mishra said:
1 decade ago
Thanks sir realy understoodable answer. Thank you so much.
Jhansi said:
1 decade ago
Thanks alot sai.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers