Hey i think you can use this following logic.
As you want to add 0-to-1000 where each bit addintion also there.
-
total_sum=0;
-
for (input=0;input<=1000;input++) {
-
sum=0; //Every time get reset.
-
-
for (i=0;i<4;i++) {
-
sum=sum+ (input >> (i*4))%(10) ;
-
}
-
total_sum=total_sum+sum;
-
}
Note: total_sum= Sum of all 0-to-1000 with each bit addition.
sum=sum of each number with bit addition [ex.945 will reperesent 18]
>>( 4*i) = will do the bit shift and with modulo it will give the that integer
value.
Ex. for 945 the for loop will executed 3 times
First i=0, No shift so remainder =5
Second i=1, Shift by 4 bit so the Number is now 0094,Remainder gives 4
Third i=2, (094) Shifted by 4 bit so the Number is now 0009,Remainder
gives 9.
Just run the code and see whether its working. :) :D :p