Jan 20, 2012 #1 varunme Advanced Member level 3 Joined Aug 10, 2011 Messages 741 Helped 17 Reputation 36 Reaction score 17 Trophy points 1,318 Activity points 5,764 How can we take the number of "high"s in a binary number ? like 0b10100000 -> 2 0b01000101 -> 3 0b01111000 -> 4
How can we take the number of "high"s in a binary number ? like 0b10100000 -> 2 0b01000101 -> 3 0b01111000 -> 4
Jan 20, 2012 #2 engshahrul Banned Joined Apr 27, 2009 Messages 329 Helped 98 Reputation 196 Reaction score 94 Trophy points 1,308 Location Malaysia Activity points 0 You need to check every bit, then if bit high, increment counter. Code: j=0; for(i=0 ; i < 8 ; i ++) if(((DATA>>i)&1) == 1) j++;
You need to check every bit, then if bit high, increment counter. Code: j=0; for(i=0 ; i < 8 ; i ++) if(((DATA>>i)&1) == 1) j++;
Jan 20, 2012 #3 srhoar Advanced Member level 4 Joined Jul 12, 2002 Messages 119 Helped 22 Reputation 44 Reaction score 10 Trophy points 1,298 Location The center of the World - Dallas Activity points 657 Try these two link: they will answer this and hopefully future questions that may arise. Bit Twiddling Hacks Fast Bit Counting Red
Try these two link: they will answer this and hopefully future questions that may arise. Bit Twiddling Hacks Fast Bit Counting Red
Jan 25, 2012 #4 varunme Advanced Member level 3 Joined Aug 10, 2011 Messages 741 Helped 17 Reputation 36 Reaction score 17 Trophy points 1,318 Activity points 5,764 srhoar, Very nice links, I didnt got any links like this before
Jan 31, 2012 #5 varunme Advanced Member level 3 Joined Aug 10, 2011 Messages 741 Helped 17 Reputation 36 Reaction score 17 Trophy points 1,318 Activity points 5,764 engshahrul said: You need to check every bit, then if bit high, increment counter. Code: j=0; for(i=0 ; i < 8 ; i ++) if(((DATA>>i)&1) == 1) j++; Click to expand... what can be the problem with the bel Code: void main() { port_B =portb; DATA = port_B; while (1) { if (UART1_Data_Ready()) { j=0; for(i=0 ; i < 8 ; i ++) { if(((DATA>>i)&1) == 1) j++; } m=Int2Str(j); UART1_Write(m); } } } used Code: UART1_Write(Lo(J)); UART1_Write(Hi(J)); too, but no luck Last edited: Jan 31, 2012
engshahrul said: You need to check every bit, then if bit high, increment counter. Code: j=0; for(i=0 ; i < 8 ; i ++) if(((DATA>>i)&1) == 1) j++; Click to expand... what can be the problem with the bel Code: void main() { port_B =portb; DATA = port_B; while (1) { if (UART1_Data_Ready()) { j=0; for(i=0 ; i < 8 ; i ++) { if(((DATA>>i)&1) == 1) j++; } m=Int2Str(j); UART1_Write(m); } } } used Code: UART1_Write(Lo(J)); UART1_Write(Hi(J)); too, but no luck
Feb 1, 2012 #6 K karthikkrv85 Full Member level 3 Joined Nov 9, 2009 Messages 172 Helped 17 Reputation 34 Reaction score 17 Trophy points 1,298 Location bangalore Activity points 2,356 varunme said: How can we take the number of "high"s in a binary number ? like 0b10100000 -> 2 0b01000101 -> 3 0b01111000 -> 4 Click to expand... hai varun you can use this method also while((x) & (x-1)) { count++; x = x-1; } Click to expand...
varunme said: How can we take the number of "high"s in a binary number ? like 0b10100000 -> 2 0b01000101 -> 3 0b01111000 -> 4 Click to expand... hai varun you can use this method also while((x) & (x-1)) { count++; x = x-1; } Click to expand...