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;
}
4 1 8 1
4 >> 1 8 >> 1
2 >> 4 Garbage value >> Garbage value
2 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 3 of 4.

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.

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.

Prat said:   9 years ago
Why garbage value? Please explain clearly.

Krutika said:   9 years ago
Thank you @Mayur.

Praba said:   9 years ago
What is mean by rest is garbage?

Anushka said:   9 years ago
But in the right shift. Spaces are filled with 1. Then how 0010 can occur?

Suyog said:   9 years ago
Can anybody explain about garbage?

Saikumar said:   9 years ago
Thank you @Neetu and @Teja.

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.

Shree said:   8 years ago
Thanks @Neetu.


Post your comments here:

Your comments will be displayed after verification.