getbits added
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Tue, 01 Jun 2010 23:40:56 +0200
changeset 130 f4b424b93e45
parent 129 d82830a449a9
child 131 b5ad49852adc
getbits added committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
getbits.c
--- a/Makefile
+++ b/Makefile
@@ -74,7 +74,7 @@
 TARGET += cppdatabase
 TARGET += pipe
 TARGET += compliteral
-
+TARGET += getbits
 
 .SUFFIXES: .c .cc .asm
 
@@ -338,6 +338,10 @@
 	@echo Linking $<...
 	@$(CC) -Wall -O2 -g -ggdb $(LDFLAGS) $< -o $@
 
+getbits: getbits.o
+	@echo Linking $<...
+	@$(CC) -Wall -O2 -g -ggdb $(LDFLAGS) $< -o $@
+
 
 .PHONY: beauty clean uninstall
 
new file mode 100644
--- /dev/null
+++ b/getbits.c
@@ -0,0 +1,44 @@
+/**
+ * getbits.c
+ * Copyright (C) 2010 Markus Broeker
+ *
+ */
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+
+#define BITS 64
+
+char *getbits (unsigned long long what)
+{
+    static char byte[BITS + 1];
+
+    short i = BITS;
+
+    memset (byte, '0', BITS);
+
+    while (what > 0) {
+        byte[--i] = (what % 2) ? '1' : '0';
+        what >>= 1;
+    }
+    byte[BITS] = '\0';
+
+    return byte;
+}
+
+int main (void)
+{
+    unsigned int i;
+    for (i = 1; i < 16; i++)
+        printf ("%s\n", getbits (i));
+
+    printf ("%s\n", getbits (USHRT_MAX));
+    printf ("%s\n", getbits (UINT_MAX));
+    printf ("%s\n", getbits (ULONG_MAX));
+
+#ifdef ULLONG_MAX
+    printf ("%s\n", getbits (ULLONG_MAX));
+#endif
+
+    return 0;
+}