Similar to c / C ++ / C #, if so, you change ... just go to N-1 bits from 0 and use sum+=(value>>i)&1
Those. you always check the last bit / right bit, but move the binary representation of the number to the right for each iteration until you have more bits to check.
Also think about signed / unsigned and any integer format. But you do not indicate how this should be considered in the question.
stefan
source share