Advertisement

binary representation

Started by August 18, 2000 03:47 PM
5 comments, last by Virus 24 years, 4 months ago
Could you please help me answer this question, it means a lot to me. Write a function CountOnes which takes an unsigned integer and returns the number of 1''s in its binary representation. I need this very badly, please at least give a hint, or show me some source code. C/C++ Thank you very much for your time.
||||-- Our creation is the transformation of one. --|||
Let''s see what you have so far. Post your ideas from what you have researched to solve the problem, and maybe some code you''ve tried to write. I''m sorry but I ain''t doing any homeworks.

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Advertisement
A hint, yes.

Okay, here''s what you''d do:

#ifdef WINDOWS
#define NUMBER_OF_BITS 32
#else
#define NUMBER_OF_BITS 16
#endif

counter = 0
for i = 0 to NUMBER_OF_BITS-1
if input_number AND 2^i then
counter = counter + 1
goto for

Now counter contains the number of 1''s that you have in input_number.

Hope this helps you some!

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
I''m not asking you to answer the whole question, i just need a hint, or an idea, or some kind of help.



||||-- Our creation is the transformation of one. --|||
||||-- Our creation is the transformation of one. --|||
Yes, thank you Gladiator.

||||-- Our creation is the transformation of one. --|||
||||-- Our creation is the transformation of one. --|||
More ideas? commentaries, suggestions?

||||-- Our creation is the transformation of one. --|||
||||-- Our creation is the transformation of one. --|||
Advertisement
What are you talking about? I gave you the whole algorithm in pseudo code, you just have to implement it in your language of preference.

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

This topic is closed to new replies.

Advertisement