yuwaraj
October 21st, 2004, 08:11 AM
Hi All,
If i have to find how may bits are set in any given number ,for this i have found one pattern
i.e.
2 = 2^1 = 1
4 = 2^2 = 1
5 = 2^2+1 = 2
6 = 2^2+2^1 = 2
.
.
11 = 2^3+2^1+1 = 3
.
.
19= 2^4+2^1+1 =3
so on
First thing i am not sure whether this method is appropriate or not , if yes then to implement
it i have to keep on dividing with highest power of 2 (two) which is minimum than given no. , and continue
with the process with remender till my remender is 1 .
But then real problem is how do i know what is highest power of 2 which is less than this given
number.If any other method please let me know.
Thanx in advance
If i have to find how may bits are set in any given number ,for this i have found one pattern
i.e.
2 = 2^1 = 1
4 = 2^2 = 1
5 = 2^2+1 = 2
6 = 2^2+2^1 = 2
.
.
11 = 2^3+2^1+1 = 3
.
.
19= 2^4+2^1+1 =3
so on
First thing i am not sure whether this method is appropriate or not , if yes then to implement
it i have to keep on dividing with highest power of 2 (two) which is minimum than given no. , and continue
with the process with remender till my remender is 1 .
But then real problem is how do i know what is highest power of 2 which is less than this given
number.If any other method please let me know.
Thanx in advance