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.
Sai said:
1 decade ago
Here mask means unsigned int i.e no negative values....mask 01 means 32 bit value.it is "00000000000000000000000000000001"
c=48 it is written as "00000000000000000000000000110000"
now "or" operation is performed then value becomes 49,
mask=mask<<1 means it shifts 1 to 1-bit before then it becomes "000000000000000000000000000000010".now do o operation with 32digit binary bit of 48.
Now value becomes 50.again mask=mask<<1 "00000100"(last 8bits)similarly do till for loop fails..then we get values as 49,50,52,56,48....the %c converts int to char thatmeans it prints the ASCII values of these nunbers ASCII of 48=0,49=1,50=2......57=9..replace the values u get o/p as 12480......
c=48 it is written as "00000000000000000000000000110000"
now "or" operation is performed then value becomes 49,
mask=mask<<1 means it shifts 1 to 1-bit before then it becomes "000000000000000000000000000000010".now do o operation with 32digit binary bit of 48.
Now value becomes 50.again mask=mask<<1 "00000100"(last 8bits)similarly do till for loop fails..then we get values as 49,50,52,56,48....the %c converts int to char thatmeans it prints the ASCII values of these nunbers ASCII of 48=0,49=1,50=2......57=9..replace the values u get o/p as 12480......
Afsheen said:
1 decade ago
Can anyone please explain this. ?
Ashwin said:
1 decade ago
Explanation. Please?
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
Sanjana said:
1 decade ago
Thanks amey your explanation is very good.
K.Harika said:
1 decade ago
Please give the correct answer properly.I can't understand this program.
Rahul Garg said:
1 decade ago
Now i am discuss about output 12480 because:
char c=48 ;its means its char value is 0
int mask=1;
there for:
c|mask(in case of%c)=48(110000)|1(000001)=49(its char value)=1
mask=mask<<1(use formula mask*2^1) mask=2;
now mask value is 2 in loop
therefor
c|mask= 110000
or 000010
---------
110010=50(its char value)=2
mask=2*2^1(from formula)new mask value=4
c|mask= 110000
000100
----------
110100=52 its char value=4
mask=4*2^1=8;
continue....till condition false
output=124800
char c=48 ;its means its char value is 0
int mask=1;
there for:
c|mask(in case of%c)=48(110000)|1(000001)=49(its char value)=1
mask=mask<<1(use formula mask*2^1) mask=2;
now mask value is 2 in loop
therefor
c|mask= 110000
or 000010
---------
110010=50(its char value)=2
mask=2*2^1(from formula)new mask value=4
c|mask= 110000
000100
----------
110100=52 its char value=4
mask=4*2^1=8;
continue....till condition false
output=124800
Nithy said:
1 decade ago
Doesn't masking mean AND operation?
Gayatree verma said:
1 decade ago
Thanks amey and sai.
Ruella said:
1 decade ago
Amey that was impressive and neat! thanks
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers