C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 6)
6.
What will be the output of the program?
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
Discussion:
36 comments Page 1 of 4.
Rather said:
6 years ago
Hello All.
For people who thinks why garbage values get printed , let us look at this ststement
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
We have four %d's, means we should have four variables to print but here we have only two representing first two :ie 4>>1 & 8>>1, last two format specifiers have no variables listed to print , so in such cases, it will print them as garbage values.
For people who thinks why garbage values get printed , let us look at this ststement
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
We have four %d's, means we should have four variables to print but here we have only two representing first two :ie 4>>1 & 8>>1, last two format specifiers have no variables listed to print , so in such cases, it will print them as garbage values.
(3)
Mayur said:
1 decade ago
4 = 0100 now shift to right 1 time. It will become 0010 = 2.
Then << is print on screen (becoz every special symbol or character print as well as written in double cotation " " )
Then it will print
8 = 1000 now shift to right 1 time. It will become 0100 = 4.
Then print Garbage value >> Garbage value
So,
Output will
2 >> 4 Garbage value >> Garbage value
Then << is print on screen (becoz every special symbol or character print as well as written in double cotation " " )
Then it will print
8 = 1000 now shift to right 1 time. It will become 0100 = 4.
Then print Garbage value >> Garbage value
So,
Output will
2 >> 4 Garbage value >> Garbage value
Teja said:
1 decade ago
@Anusha,
Given code is
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
First we got the output from 4>>1,8>>1 is 2,4 directly.
But in printf() four %d's are given.so 2,4 assigns to first two %d's and the remaining two %d's will show the garbage values.
Got it?
Given code is
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
return 0;
}
First we got the output from 4>>1,8>>1 is 2,4 directly.
But in printf() four %d's are given.so 2,4 assigns to first two %d's and the remaining two %d's will show the garbage values.
Got it?
(1)
Adam said:
9 years ago
#include<stdio.h>
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
printf("%d >> %d >> \n", 4 >> 1, 8 >> 1);
printf("%d %d \n", 4 >> 1, 8 >> 1);
return 0;
}
Try this.
int main()
{
printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
printf("%d >> %d >> \n", 4 >> 1, 8 >> 1);
printf("%d %d \n", 4 >> 1, 8 >> 1);
return 0;
}
Try this.
Sundar said:
1 decade ago
I got the following outputs:
In GCC (32-bit platform): 2 >> 4 134513662 >> 134513904
Turbo c (16 bit platorm): 2 >> 4 0 >> 344
Therefore, 2 >> 4 garbagevalue >> garbagevalue is the correct answer.
In GCC (32-bit platform): 2 >> 4 134513662 >> 134513904
Turbo c (16 bit platorm): 2 >> 4 0 >> 344
Therefore, 2 >> 4 garbagevalue >> garbagevalue is the correct answer.
Gowda said:
1 decade ago
Here is my code.
#include<stdio.h>
int main ()
{
int i ;
for (i=0;i<3;++i)
printf ("%d\n", i) ;
return 0;
}
I am getting output as 0 1 2.
Can any one explain why am not getting from 1 as I used pre-increment operator.
#include<stdio.h>
int main ()
{
int i ;
for (i=0;i<3;++i)
printf ("%d\n", i) ;
return 0;
}
I am getting output as 0 1 2.
Can any one explain why am not getting from 1 as I used pre-increment operator.
Vishu said:
9 years ago
@Nizamuddin,
Array always represents starting from '0' position so there is two matrices represented as a a[0] & a[1].
so there is no a[2] so it shows a value as '0'.
If I made any mistake correct me. Thank you.
Array always represents starting from '0' position so there is two matrices represented as a a[0] & a[1].
so there is no a[2] so it shows a value as '0'.
If I made any mistake correct me. Thank you.
Jay said:
1 decade ago
First it solve 4>>1 and then solve 8>>1.
Answer is 2 and 4.
Then it print the value.
But here %d is 4 times in printf("").
So it print only two values and other two are garbage value.
Answer is 2 and 4.
Then it print the value.
But here %d is 4 times in printf("").
So it print only two values and other two are garbage value.
Reenu said:
1 decade ago
@maya.
As the number gave after >> is 1..we have right shift one place..If that number is 2(>> 2)then we have to right shift 2 place..It depends on the number given after >>.
As the number gave after >> is 1..we have right shift one place..If that number is 2(>> 2)then we have to right shift 2 place..It depends on the number given after >>.
Sai said:
6 years ago
4>>1 means divide 4 by 2 power 1 and 8>>1 means divide 8 by 2 power 1 then you will get answer as 2 and 4 if it is less then less then multiply by 2.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers