*
za******@gmail.com:
I have a random inteeger that i need to check if meet a certain
criteria. The criteria is that when converted to binary it consist only
of 1's, no 0's.
If that criteria is met i allso need to know how many 1's it consist
of.
Values that would meet the first criteria is fx.
3
7
15
and the output i need will be:
3 = 2
7 = 3
15 = 4
it all seems fairly simple, but i cant seem to figure out how to do it
in a proper way. I could probably do some magic with log2, but that is
not the way i want to do it.
Any suggestions?
First, this is obviously homework, and it would be nice to mention that.
clc++-ers generally don't do other's homework, but we can help with
concrete C++ problems.
But you don't post any concrete C++ problem.
Now if you'd posted this in [comp.programming] it would have been
on-topic there (but also there with the caveat about homework), and
perhaps you'd receive answers such as "check that x+1 has exactly one
1-bit", which reduces the problem to counting 1-bits.
For counting 1-bits in almost any programming language, you can use
repeated division by 2 and checking the remainder each time. In C++ you
can also use the right-shift operator >>. And many other ways; it may
be a good idea to post again when you have some code to present.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?