C Programming - Bitwise Operators - Discussion

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;
}

[A]. 4 1 8 1
[B]. 4 >> 1 8 >> 1
[C]. 2 >> 4 Garbage value >> Garbage value
[D]. 2 4

Answer: Option C

Explanation:

No answer description available for this question.

Neetu said: (Aug 21, 2010)  
4 = 0100 now shift to right 1 time. It will become 0010 = 2.

8 = 1000 now shift to right 1 time. It will become 0100 = 4.

Rest is garbage, so the answer is C.

Sundar said: (Mar 17, 2011)  
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.

Divya S said: (May 26, 2011)  
Yes neetu understood thanks

Maruti Shrivastava said: (Jul 6, 2011)  
Very thanks to you neetu.

Mukesh said: (Jul 16, 2011)  
2 >> 4 rest depends on the compiler..

Deepika said: (Aug 6, 2011)  
Thanks Neetu.....

Maya said: (Aug 13, 2011)  
Why shouLd we right shift to one place?

Reenu said: (Aug 22, 2011)  
@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 >>.

Nizamuddin said: (Nov 4, 2011)  
void main()
{
int x[2][2][2]={1,2,3,4,5,6,7,8}
int *p;
p=&x[2][2][2];
printf("%d",*p);
}
ans is 0 (zero) why

Anusha said: (Nov 24, 2011)  
Why do we get garbage values can you explain?

Jayesh said: (Feb 7, 2012)  
What is GCC 32 bit platform, please explain me.

Teja said: (May 3, 2012)  
@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?

Bhagyshri said: (Jun 3, 2012)  
gcc 32 is compiler in unix o.s.

Gowda said: (Jun 29, 2012)  
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.

Maddy said: (Jul 20, 2012)  
Please make the condition as for(i=1;i<=3;i++) , pre/post increment has no significance here.

Chinnu said: (Aug 23, 2012)  
What is mean by rest is garbage. ?

Balayogi said: (Aug 25, 2012)  
Post or pre increments gives the same result when they are free,like i++ and ++i(i.e.without assignment)

Kuljeet said: (Aug 27, 2012)  
@Chinnu- Rest is garbage means, the other %d's in the printf fuction will print any arbitrary value.. thats known as garbage value..

Mayur said: (Nov 15, 2012)  
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

Sachin said: (Nov 30, 2014)  
@Gowda.

Arrange your number in three arrays see there is no number for 3rd row, 3rd column. So it's partially initialized which has default value zero.

Jay said: (Feb 7, 2015)  
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: (Mar 3, 2016)  
#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: (May 17, 2016)  
Why garbage value? Please explain clearly.

Krutika said: (Jul 4, 2016)  
Thank you @Mayur.

Praba said: (Sep 8, 2016)  
What is mean by rest is garbage?

Anushka said: (Sep 11, 2016)  
But in the right shift. Spaces are filled with 1. Then how 0010 can occur?

Suyog said: (Nov 6, 2016)  
Can anybody explain about garbage?

Saikumar said: (Dec 16, 2016)  
Thank you @Neetu and @Teja.

Vishu said: (Jan 16, 2017)  
@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: (Feb 26, 2017)  
Thanks @Neetu.

Ashwini said: (Jul 10, 2017)  
Please explain, why garbage value occur?

Vandana said: (Dec 27, 2017)  
Thank you for all the given explanation.

Malu said: (Feb 23, 2018)  
@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?

Harshal said: (Sep 4, 2018)  
Thanks for explaining the answer.

Sai said: (May 3, 2019)  
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.

Rather said: (Jan 8, 2020)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.