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.
Gayatree verma said:
1 decade ago
Thanks amey and sai.
Ruella said:
1 decade ago
Amey that was impressive and neat! thanks
Vraj shah said:
1 decade ago
Thanks amey :).
Pramod Soni said:
1 decade ago
Thank you very much Amey.
Amey said:
1 decade ago
c=110000
Mask=000001
In for loop
i=1
Print 110000 | 000001 i.e. 110001=49 i.e. ascii for '1'
Hence printing '1'
Now mask<<1 hence mask=000010
i=2
Print 110000 | 000010 i.e. 110010=50 i.e. ascii for '2'
Hence printing '2'
Now mask<<1 hence mask=000100
i=3
Print 110000 | 000100 i.e. 110100=52 i.e. ascii for '4'
Hence printing '4'
Now mask<<1 hence mask=001000
i=4
Print 110000 | 001000 i.e. 111000=56 i.e. ascii for '8'
Hence printing '8'
Now mask<<1 hence mask=010000
i=5
Print 110000 | 010000 i.e. 110000=48 i.e. ascii for '0'
Hence printing '0'
Now mask<<1 hence mask=001000
Mask=000001
In for loop
i=1
Print 110000 | 000001 i.e. 110001=49 i.e. ascii for '1'
Hence printing '1'
Now mask<<1 hence mask=000010
i=2
Print 110000 | 000010 i.e. 110010=50 i.e. ascii for '2'
Hence printing '2'
Now mask<<1 hence mask=000100
i=3
Print 110000 | 000100 i.e. 110100=52 i.e. ascii for '4'
Hence printing '4'
Now mask<<1 hence mask=001000
i=4
Print 110000 | 001000 i.e. 111000=56 i.e. ascii for '8'
Hence printing '8'
Now mask<<1 hence mask=010000
i=5
Print 110000 | 010000 i.e. 110000=48 i.e. ascii for '0'
Hence printing '0'
Now mask<<1 hence mask=001000
Bhavesh said:
1 decade ago
"c|mask" what does it mean?
Ajit Yadav said:
1 decade ago
For those having problem in 5th loop.
C = 48 = 110000.
Mask= 16 = 010000.
Perform or operation and you will get 110000 i.e. 48.
And 48 in ascii is '0'.
C = 48 = 110000.
Mask= 16 = 010000.
Perform or operation and you will get 110000 i.e. 48.
And 48 in ascii is '0'.
Mun said:
1 decade ago
Left shift << is Multiply by power of 2.
So therefore ,12480 consecutively 5 times !!
So therefore ,12480 consecutively 5 times !!
Ravi said:
1 decade ago
Thanks sai.
Rohit said:
1 decade ago
@Sai. Thank you very nuch.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers