]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_tail.c
worked abit on the type_to_bianary functions
[16.git] / src / lib / 16_tail.c
index 50cd2e8240169a5d8f48657fe56d2bb13503e933..d8f18b9483fa429b79bab5705ae0e653e9b438af 100755 (executable)
@@ -793,37 +793,52 @@ void turboXT(byte bakapee)
 }\r
 #endif\r
 \r
-const char *word_to_binary(word x)\r
+//from https://stackoverflow.com/questions/18327439/printing-binary-representation-of-a-char-in-c\r
+const char *word_to_binary(int x)\r
 {\r
        static char b[17];\r
        int z;\r
 \r
        b[0] = '\0';\r
-       for (z = 16; z > 0; z >>= 1)\r
+       for (z = 256; z > 0; z >>= 1)\r
        {\r
+///            printf("                z=%u    b=%u    x=%u\n", z, b, x);\r
                strcat(b, ((x & z) == z) ? "1" : "0");\r
        }\r
        return b;\r
 }\r
 \r
-const char *nibble_to_binary(nibble x)\r
+const char *byte_to_binary(int x)\r
 {\r
        static char b[9];\r
        int z;\r
 \r
        b[0] = '\0';\r
-       for (z = 8; z > 0; z >>= 1)\r
+       for (z = 128; z > 0; z >>= 1)\r
        {\r
                strcat(b, ((x & z) == z) ? "1" : "0");\r
        }\r
        return b;\r
 }\r
 \r
-const char *boolean_to_binary(boolean x)\r
+const char *nibble_to_binary(int x)\r
 {\r
        static char b[9];\r
        int z;\r
 \r
+       b[0] = '\0';\r
+       for (z = 8; z > 0; z >>= 1)\r
+       {\r
+               strcat(b, ((x & z) == z) ? "1" : "0");\r
+       }\r
+       return b;\r
+}\r
+\r
+const char *boolean_to_binary(int x)\r
+{\r
+       static char b[3];\r
+       int z;\r
+\r
        b[0] = '\0';\r
        for (z = 1; z > 0; z >>= 1)\r
        {\r
@@ -832,13 +847,35 @@ const char *boolean_to_binary(boolean x)
        return b;\r
 }\r
 \r
+void wordtest()\r
+{\r
+       word pee;\r
+       printf("wordtest\n");\r
+       /* word to binary string */\r
+       for(pee=0;pee<280;pee++)\r
+               printf("        %u      %s\n", pee, word_to_binary(pee));\r
+       printf("        sizeof(word)=%s\n", word_to_binary(sizeof(word)));\r
+       printf("end of word test\n");\r
+}\r
+\r
+void bytetest()\r
+{\r
+       byte pee;\r
+       printf("bytetest\n");\r
+       /* byte to binary string */\r
+       for(pee=0;pee<18;pee++)\r
+               printf("        %u      %s\n", pee, byte_to_binary(pee));\r
+       printf("        sizeof(byte)=%s\n", byte_to_binary(sizeof(byte)));\r
+       printf("end of byte test\n");\r
+}\r
+\r
 void nibbletest()\r
 {\r
        nibble pee;\r
        printf("nibbletest\n");\r
        /* nibble to binary string */\r
        for(pee=0;pee<18;pee++)\r
-               printf("        %u %s\n", pee, nibble_to_binary(pee));\r
+               printf("        %u      %s\n", pee, nibble_to_binary(pee));\r
        printf("        sizeof(nibble)=%s\n", nibble_to_binary(sizeof(nibble)));\r
        printf("end of nibble test\n");\r
 }\r
@@ -849,7 +886,7 @@ void booleantest()
        printf("booleantest\n");\r
        /* boolean to binary string */\r
        for(pee=0;pee<4;pee++)\r
-               printf("        %u %s\n", pee, boolean_to_binary(pee));\r
+               printf("        %u      %s\n", pee, boolean_to_binary(pee));\r
        printf("        sizeof(boolean)=%s\n", boolean_to_binary(sizeof(boolean)));\r
        printf("end of boolean test\n");\r
 }\r