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 1 of 4.

Rather said:   5 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.
(2)

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)

Harshal said:   6 years ago
Thanks for explaining the answer.

Malu said:   7 years ago
@Neetu.

Why we should consider the rest value as garbage I can take it directly as 2 4 why should go for garbage value. Could you please explain me?

Vandana said:   7 years ago
Thank you for all the given explanation.

Ashwini said:   7 years ago
Please explain, why garbage value occur?

Shree said:   8 years ago
Thanks @Neetu.

Vishu said:   8 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.

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

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


Post your comments here:

Your comments will be displayed after verification.