diff options
author | justanothercatgirl <sotov@twistea.su> | 2024-12-12 09:34:08 +0300 |
---|---|---|
committer | justanothercatgirl <sotov@twistea.su> | 2024-12-12 09:34:08 +0300 |
commit | e9bbfdadb54d91379385f586f63e0f004bd79c50 (patch) | |
tree | 82ac46a3b00cfaeda44b5737667d927802d44cb8 /7_1 | |
parent | 7f1951a1ef8adca7fdd29fae185931b69b50e4d3 (diff) |
Cleaned up 7_1
Diffstat (limited to '7_1')
-rw-r--r-- | 7_1/Makefile | 38 | ||||
-rw-r--r-- | 7_1/main.c | 45 |
2 files changed, 45 insertions, 38 deletions
diff --git a/7_1/Makefile b/7_1/Makefile index 80b3f39..f7aaf17 100644 --- a/7_1/Makefile +++ b/7_1/Makefile @@ -1,29 +1,33 @@ +CFLAGS += -std=c89 + all: test .PHONY: test clean test: task7_1 - ./$< 69 - ./$< 429 - ./$< 828 - ./$< 425729 - ./$< 235723572579 - ./$< 000111222333 - ./$< 1048575 - ./$< 1048576 - ./$< -31273891273891273 - ./$< -000000000000000000 - ./$< -2313423 - ./$< -123 + - ./$< + @./$< 69 + @./$< 429 + @./$< 828 + @./$< 425729 + @./$< 235723572579 + @./$< 000111222333 + @./$< 1048575 + @./$< 1048576 + @./$< -31273891273891273 + @./$< -000000000000000000 + @./$< -2313423 + @./$< -123 - ./$< -9223372036854775809 - ./$< -9223372036854775807 + @./$< -9223372036854775807 - ./$< -9223372036854775808 - ./$< -1048576 - ./$< -1048575 + @./$< -1048576 + @./$< -1048575 task7_1: main.c - cc -o task7_1 main.c - + $(CC) -o $@ $< $(CFLAGS) +clean: + $(RM) task7_1 @@ -1,20 +1,23 @@ #include<stdlib.h> #include<stdio.h> -/* бля какой же код охуенный */ -/* ломается, если x = 0x8000000000000000 */ +#define RED "\x1b[91m" +#define GRN "\x1b[92m" +#define RST "\x1b[0m" + +/* breaks (segfaults) if x = 0x8000000000000000 */ char* tobin(long int x) { size_t i = 0, j = 0; char buffer[64]; char *ret; char sgn = x < 0; - x *= ((x > 0) << 1) - 1; - for (; x; x >>= 1) buffer[i++] = '0' + (x & 1); - ret = calloc(i + 1 + sgn, sizeof *ret); - ret[0] = '-'; - j += sgn; - while (i) ret[j++] = buffer[--i]; + x *= ((x > 0) << 1) - 1; /* Negate the number without if statement */ + for (; x; x >>= 1) buffer[i++] = '0' + (x & 1); /* Write the bits to the buffer */ + ret = calloc(i + 1 + sgn, sizeof *ret); /* Allocate an arary for the number */ + ret[0] = '-'; /* Always write minus as the first sign*/ + j += sgn; /* If sign is negative, increment j so that '-' is not overwritten */ + while (i) ret[j++] = buffer[--i]; /* reverse copy buffer into ret */ return ret; } @@ -22,24 +25,24 @@ char* tobin(long int x) { int main(int argc, char* argv[]) { long number; char *bin; - + if (argc != 2) { - fprintf(stderr, "\x1b[91mError: Invalid amount of arguments\x1b[0m\n" - "Usage: \x1b[92m%s <number>\n\x1b[0m", argv[0]); - return 1; + fprintf(stderr, RED "Error: Invalid amount of arguments\n" RST "Usage: " GRN "%s <number>\n" RST , argv[0]); + return EXIT_FAILURE; } number = atol(argv[1]); - if (number == 0) { - puts("Binary for 0: 0"); - return 0; - } + + /* Since I am negating a number using the branchless trick, + * Multiplying this number by -1 still gives you a negative number because of the overflow + * Unfortunately this has to be handled separately */ if (number == 0x8000000000000000) { - puts("Aha, I see what you did there)"); - return 1; - } + puts(RED "Error: " RST "The number does not fit in 64 bits"); + return EXIT_FAILURE; + } + bin = tobin(number); - printf("Binary for %li: %s\n", number, bin); + printf("Binary for %-20li: %s\n", number, bin); free(bin); - return 0; + return EXIT_SUCCESS; } |