/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdint.h>
#include <stdio.h>
typedef uint32_t u32_t;
void printBits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
int i, j;
for (i = size-1; i >= 0; i--) {
for (j = 7; j >= 0; j--) {
byte = (b[i] >> j) & 1;
printf("%u", byte);
}
}
puts("");
}
u32_t FCN_count_trailing_zeros(u32_t x)
{
u32_t c = 32u; // c will be the number of zero bits on the right
if (x != 0u) // if n == 0, skip and return c = 32
{
x &= (unsigned)-(signed)x;
c = 31u;
if ((x & 0x0000FFFFu) != 0u)
{
c = 15u;
}
if ((x & 0x00FF00FFu) != 0u)
{
c -= 8u;
}
if ((x & 0x0F0F0F0Fu) != 0u)
{
c -= 4u;
}
if ((x & 0x33333333u) != 0u)
{
c -= 2u;
}
if ((x & 0x55555555u) != 0u)
{
c -= 1u;
}
}
return c;
}
int main()
{
for (u32_t i = 0; i <= 256; i++) {
u32_t j = FCN_count_trailing_zeros(i);
printf("%d: %d, ", i, j);
printBits(4, &i);
printf("\n");
}
return 0;
}