c programm code that find the no of zeros in 8 bit deta item.

Discussion in 'Embedded Systems and Microcontrollers' started by kof97, May 8, 2010.

  1. kof97

    Thread Starter New Member

    May 8, 2010
    9
    0
    plz tell me c programm code that finds the number of zeros in 8 bit deta item.
     
  2. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    Look up Hamming weight. For a single byte, constructing a lookup table is always an option, as it's simple and fast.
     
  3. braindead

    New Member

    May 12, 2010
    28
    0
    What do you mean with zeros? You mean bits that are zero or zeros in the decimal representation?
     
  4. braindead

    New Member

    May 12, 2010
    28
    0
    Code ( (Unknown Language)):
    1.  
    2.  
    3. uint8_t bits_that_are_zero(uint8_t num)
    4. {
    5.   return low_bits_that_are_zero(num)
    6.            + low_bits_that_are_zero(num>>4);
    7. }
    8.  
    9. uint8_t low_bits_that_are_zero(uint8_t num)
    10. {
    11.   uint8_t zeros;
    12.   num &= 0xF;
    13.  
    14.   zeros = (~num)&0x3;
    15.  
    16.   if(zeros < 2)
    17.     zeros += 1;
    18.  
    19.   if(num < 4)
    20.     zeros += 1;
    21.   else if(num >=12)
    22.     zeros -= 1;
    23.  
    24.   return zeros;
    25. }
    26.  
    27.  
     
    Last edited: May 13, 2010
Loading...