### Discussion :: Bitwise Operators - Point Out Correct Statements (Q.No.2)

Saurabh said: (Jan 21, 2011) | |

The answer is A. Let binary value of num is 10101 then the loop checks each bit on by 'if(num & 1)' and num>>=1 right shifts the num bits by 1bit |

Vijay said: (Aug 3, 2012) | |

If num>>1 means right shift one bit, but what is mean by num>>=1 ? |

Spa said: (Aug 18, 2012) | |

@vijay yes i too have d same doubt. what is mean by num>>=1 ? |

Varun said: (Aug 22, 2012) | |

I think it is short hand operator such that num=num>>1 ? |

Kadarkarai Selvam said: (Sep 14, 2012) | |

Let us take num=20 Here num>>=1 means that num=num>>1 In for loop, At num =20 ie 0001 0100 Now num&1 gives 000000 so it wont increment c Then at next loop, num= num>>1 ie 0000 1010 Now num&1 gives 0000 0000 so it wont increment c Then at next loop, num= num>>1 ie 0000 0101 Now num&1 gives 0000 0001 so it increment c Like that it goes and c increments for 2 times in this case.. Thus it gives the number of ON bits in num.. |

Ash said: (Mar 15, 2014) | |

What is the meaning of if(num&1)? |

Lohith said: (Aug 18, 2015) | |

Num &1 means doing AND operation for num and 1 for example. Num = 0010(2). 1 = 0001. -----------. 0000 (Here c will no increment and goes to for loop). For next loop right bit shift was implemented. Num = 0001(2). 1 = 0001. -----------. 0001 (here c will be increment and goes to for loop). This process was continued up to num times. if (0) { Terminated the if condition and goes to for loop. } if (1) { Execute the if condition. } |

Aami said: (Mar 4, 2016) | |

No termination condition for FOR LOOP? Will it runs infinitely. |

Mihir said: (Aug 16, 2016) | |

Thanks @Kadarkarai Selvam. |

Suheel said: (Jan 28, 2020) | |

What "T+=i&1;" means? |

Pavan said: (Sep 3, 2020) | |

T+=i&1 is same as T=T+(i&1) i.e,adding result (i&1) to initial value of T and again storing it in T. |

Sibysagar said: (Mar 8, 2022) | |

@Aami. As we num>>=1 right shift the value of num, when it reaches binary value 0000000000000000 it gets out of for loop. If we give num=4; then the loop runs for 3 three times and the num value will be; 1st time entering loop num will be 4 (0000000000000100) after the right shift it will be(0000000000000010). 2nd time entering loop num will be 2 (0000000000000010) after the right shift it will be(0000000000000001). 3rd time entering loop num will be 1 (0000000000000001) after the right shift it will be (0000000000000000). 4th time entering loop num will be 0 (0000000000000000) so code will exit for a loop. |

#### Post your comments here:

Name *:

Email : (optional)

» Your comments will be displayed only after manual approval.