From 7be9d0f0496975b80f80accf1a502f3fa44a848e Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Fri, 7 Apr 2017 16:32:44 +0200 Subject: [PATCH] bitops: add ilog2() --- Makefile | 1 + include/bitops.h | 3 +++ source/bitops.c | 11 +++++++++++ 3 files changed, 15 insertions(+) create mode 100644 include/bitops.h create mode 100644 source/bitops.c diff --git a/Makefile b/Makefile index 60acaea..4d229a0 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ CFLAGS += -Iinclude CFLAGS += -Wall -Wundef -Wextra -Wshadow -Wimplicit-function-declaration CFLAGS += -Wredundant-decls -Wmissing-prototypes -Wstrict-prototypes +obj-y += source/bitops.o obj-y += source/bitset.o obj-y += source/main.o obj-y += source/shell/cmd.o diff --git a/include/bitops.h b/include/bitops.h new file mode 100644 index 0000000..abee5a9 --- /dev/null +++ b/include/bitops.h @@ -0,0 +1,3 @@ +#pragma once + +unsigned ilog2(unsigned n); diff --git a/source/bitops.c b/source/bitops.c new file mode 100644 index 0000000..59048e2 --- /dev/null +++ b/source/bitops.c @@ -0,0 +1,11 @@ +#include + +unsigned ilog2(unsigned n) +{ + unsigned ret = 0; + + while (n >>= 1) + ++ret; + + return ret; +}