+ 1

Please explain the logic of this program

#include <iostream> using namespace std; int main() { int n=28; int value=0; int i=0; while(n>=1) { value=value+(n%2)*2^i; n=n/2; i=i+2; cout<<value; } return 0; }

23rd Mar 2017, 4:57 PM
Nitish Kulkarni
Nitish Kulkarni - avatar
1 Answer
+ 3
value=value+(n%2)*2^i Let n = 13 and i = 0 Step 1: On solving n%2, we get 13%2 = 1; Step 2: On multiplying above result by 2, we get 1*2 = 2 Step 3: Now xor the result in step 2 with value of i xor return 1 when bit positions in both operands are different, otherwise 0. 2 = 00000010 0 = 00000000 2^0 = 00000010 = 2 Step 4: Accumulate the result of step 3 into value. Rest you may solve by your own.
23rd Mar 2017, 6:06 PM
à€Šà„‡à€”à„‡à€‚à€Šà„à€° à€źà€čà€Ÿà€œà€š (Devender)
à€Šà„‡à€”à„‡à€‚à€Šà„à€° à€źà€čà€Ÿà€œà€š (Devender) - avatar