Jul 19, 2006 #1 K kad Junior Member level 2 Joined Feb 8, 2005 Messages 24 Helped 0 Reputation 0 Reaction score 0 Trophy points 1,281 Activity points 199 Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution).
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution).
Jul 19, 2006 #2 F fenugrec Member level 3 Joined Apr 30, 2006 Messages 56 Helped 3 Reputation 6 Reaction score 1 Trophy points 1,288 Activity points 1,611 What ? Is that an order? I hope you don't expect anyone to "give" you a ready-made solution... google for "parity"
What ? Is that an order? I hope you don't expect anyone to "give" you a ready-made solution... google for "parity"
Jul 20, 2006 #3 E electronrancher Advanced Member level 1 Joined Mar 24, 2002 Messages 447 Helped 46 Reputation 92 Reaction score 11 Trophy points 1,298 Activity points 6,935 not an order, just verbatim from the homework assignment - lol!
Jul 20, 2006 #4 T tronix Advanced Member level 4 Joined Jul 6, 2006 Messages 116 Helped 8 Reputation 16 Reaction score 1 Trophy points 1,298 Activity points 1,967 electronrancher said: not an order, just verbatim from the homework assignment - lol! Click to expand... Sure looks like a straight lift but cann't resist frm replying let x be d no. not equal to zero ans=1 while(x>2) { if( x%2!=0) {ans++; x--;} x=x/2; } ans will give the no. of ones TRY TO UNDERSTAND THE LOGIC AS THATS WHAT MATTERS
electronrancher said: not an order, just verbatim from the homework assignment - lol! Click to expand... Sure looks like a straight lift but cann't resist frm replying let x be d no. not equal to zero ans=1 while(x>2) { if( x%2!=0) {ans++; x--;} x=x/2; } ans will give the no. of ones TRY TO UNDERSTAND THE LOGIC AS THATS WHAT MATTERS
Jul 20, 2006 #5 polymath Full Member level 4 Joined May 2, 2006 Messages 231 Helped 28 Reputation 56 Reaction score 4 Trophy points 1,298 Location England Activity points 3,105 Hi all On average there will be 16 - the most probable prediction every time. Polymath
Jul 20, 2006 #6 T tronix Advanced Member level 4 Joined Jul 6, 2006 Messages 116 Helped 8 Reputation 16 Reaction score 1 Trophy points 1,298 Activity points 1,967 polymath said: Hi all On average there will be 16 - the most probable prediction every time. Polymath Click to expand... Why should ?
polymath said: Hi all On average there will be 16 - the most probable prediction every time. Polymath Click to expand... Why should ?
Jul 21, 2006 #7 polymath Full Member level 4 Joined May 2, 2006 Messages 231 Helped 28 Reputation 56 Reaction score 4 Trophy points 1,298 Location England Activity points 3,105 tronix said: polymath said: Hi all On average there will be 16 - the most probable prediction every time. Polymath Click to expand... Why should ? Click to expand... Why not? You have not defined your 32 bits as having any structure other than random. Read about Statistics & Probability. Noise - Long Comm. Transmissions and many data structures have equal 1s & 0s by chance or design. yes .. no .. yes .. no ... Polymath
tronix said: polymath said: Hi all On average there will be 16 - the most probable prediction every time. Polymath Click to expand... Why should ? Click to expand... Why not? You have not defined your 32 bits as having any structure other than random. Read about Statistics & Probability. Noise - Long Comm. Transmissions and many data structures have equal 1s & 0s by chance or design. yes .. no .. yes .. no ... Polymath
Jul 21, 2006 #8 T tronix Advanced Member level 4 Joined Jul 6, 2006 Messages 116 Helped 8 Reputation 16 Reaction score 1 Trophy points 1,298 Activity points 1,967 Well polymath I think here we r talking abt deterministic coding and not the random ones.
Jul 22, 2006 #9 C cfant Full Member level 3 Joined Apr 4, 2001 Messages 187 Helped 25 Reputation 50 Reaction score 6 Trophy points 1,298 Location California, USA Activity points 1,153 You can use lookup table which contains number of ones in one byte. Since the word is 4 bytes (assuming normal cpu), just add results from all four bytes. Or you can use some of these algorithms: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable
You can use lookup table which contains number of ones in one byte. Since the word is 4 bytes (assuming normal cpu), just add results from all four bytes. Or you can use some of these algorithms: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetTable