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 3 of 7.
Prajkta said:
10 years ago
Please detail explanation this program.
Mukund said:
10 years ago
48- ASCII value is 0.
48 | 01 = 49 value is 1 printed.
Mask << 1 means (mask) 1*2^(1) = 2.
Again 48|2 value 2 will printed.
Mask = 2*2 = 4.
48|4 then 4 is printed after 8 and 0 are printed.
48 | 01 = 49 value is 1 printed.
Mask << 1 means (mask) 1*2^(1) = 2.
Again 48|2 value 2 will printed.
Mask = 2*2 = 4.
48|4 then 4 is printed after 8 and 0 are printed.
Swati said:
10 years ago
How 48 ASCII is 0? Please anyone explain it?
Jitendra Malviya said:
1 decade ago
Mask is assigned 1 i.e. 00000001, means? I am not getting point.
Mukunda saini said:
1 decade ago
I think OR operation means 1+1 = Sum 0 and carry 1. So carry will add to the next position. 110000+010000 = 1000000. Please tell me how to get last one 48?
Venu Gopal said:
1 decade ago
There is nothing in left shift(<<) and right shift(>>) operators.
Let us discuss in simple manner.
1. Left Shift operator(<<):-
For example if the left shift operator in this manner i<<n the evaluation will be i<<n=i*(2^n).
2. Right shift operator(>>).
i>>n=i/(2^n).
Ex::2<<3=2*(2^3)=2*(2*2*2)=2*(8)=16.(ans).
Ex::2>>3=2/(2^3)=2/(2*2*2)=2/(8)=0.(ans).
Let us discuss in simple manner.
1. Left Shift operator(<<):-
For example if the left shift operator in this manner i<<n the evaluation will be i<<n=i*(2^n).
2. Right shift operator(>>).
i>>n=i/(2^n).
Ex::2<<3=2*(2^3)=2*(2*2*2)=2*(8)=16.(ans).
Ex::2>>3=2/(2^3)=2/(2*2*2)=2/(8)=0.(ans).
Priyanka said:
1 decade ago
For last loop.
i=5;
0011 0000
|0001 0000
=0100 0000 its 64.
how come its 48?
i=5;
0011 0000
|0001 0000
=0100 0000 its 64.
how come its 48?
Pankaj said:
1 decade ago
#include<stdio.h>
int main()
{
char c=48; // Integer value of c is 48, which belongs to char '0';
int i, mask=01; // same as mask=1;
printf("%c\n",c);
for(i=1; i<=5; i++)
{
printf("%d %d\n",c,mask); // displaying the integer values of c and mask
printf("%c\n", c|mask);
mask = mask<<1;
}
return 0;
}
Since the bitwise operator is defined on the integer values, i.e a|b, where a and b are integers. In this problem, we calculate bitwise OR on integer values and then convert it back to char as the output is required as "%c". Try the above code, Hope this will help.
int main()
{
char c=48; // Integer value of c is 48, which belongs to char '0';
int i, mask=01; // same as mask=1;
printf("%c\n",c);
for(i=1; i<=5; i++)
{
printf("%d %d\n",c,mask); // displaying the integer values of c and mask
printf("%c\n", c|mask);
mask = mask<<1;
}
return 0;
}
Since the bitwise operator is defined on the integer values, i.e a|b, where a and b are integers. In this problem, we calculate bitwise OR on integer values and then convert it back to char as the output is required as "%c". Try the above code, Hope this will help.
Tanu Priya Saxena said:
1 decade ago
Mask is declared 01. It should be read as an octal since it is starting with 0.
Jasss said:
1 decade ago
#include<stdio.h>
int main()
{
int i=4, j=8;
printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
return 0;
}
Can anyone tell me how this program will work step by step?
int main()
{
int i=4, j=8;
printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
return 0;
}
Can anyone tell me how this program will work step by step?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers