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 2 of 7.
Ramkumar said:
1 decade ago
Left shift operation like an multiples of 2. For ex. If we left shift of 2 at one time the result will be 2*2 = 4. Similarly, right shift operation like an divide by 2. For ex. If we right shift of 2 at one time the result will be 2/2 = 1.
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.
Maheswari said:
8 years ago
Yes, you can assigned integer value in char data type.
For example :
char i=21;
printf("%c",i); //it will not print anything and no error;
printf("%d",i);//it will print the value of I;
For example :
char i=21;
printf("%c",i); //it will not print anything and no error;
printf("%d",i);//it will print the value of I;
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?
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.
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.
Zhongshunchao said:
7 years ago
0011 0000 == 48 ---0
0011 0001 == 49 ---1 48|0001.
0011 0010 == 50 ---2 48|0010.
0011 0100 == 52 ---4 48|0100.
0011 1000 == 56 ---8 48|1000.
0011 0000 == 48 ---0 48|0000.
0011 0001 == 49 ---1 48|0001.
0011 0010 == 50 ---2 48|0010.
0011 0100 == 52 ---4 48|0100.
0011 1000 == 56 ---8 48|1000.
0011 0000 == 48 ---0 48|0000.
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?
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'.
Jitendra vaishnav said:
9 years ago
@Mukunda Saini.
OR operation doesn't means sum its logical or operation.
Where 1 OR 1 is 1. So there will be no carry on next position.
OR operation doesn't means sum its logical or operation.
Where 1 OR 1 is 1. So there will be no carry on next position.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers