finished signed and twos

This commit is contained in:
Rushil Umaretiya 2024-02-20 03:05:38 -05:00
parent 2af160daeb
commit 9114a8bda9
No known key found for this signature in database
GPG Key ID: 4E8FAF9C926AF959
2 changed files with 69 additions and 4 deletions

View File

@ -1,8 +1,33 @@
// PID: 9DigitPidNoSpacesOrDashes
// PID: 730677144
// I pledge the COMP 211 honor code.
#include <stdlib.h>
#include <stdio.h>
int main() {
int main(int argc, char *argv[]) {
int num = atoi(argv[1]);
if (num < -32768 || num > 32767) {
printf("not possible\n");
return 0;
}
if (argv[1][0] == "-") {
printf("1");
num = -num;
} else {
printf("0");
}
for (int i = 15; i > 0; i--) {
if (num >= (1 << i)) {
printf("1");
num -= (1 << i);
} else {
printf("0");
}
}
printf("\n");
return EXIT_SUCCESS;
}

44
twos.c
View File

@ -1,8 +1,48 @@
// PID: 9DigitPidNoSpacesOrDashes
// PID: 730677144
// I pledge the COMP 211 honor code.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
int num = abs(atoi(argv[1]));
char out[16];
if (num > 65535) {
printf("not possible\n");
return 0;
}
for (int i = 15; i >= 0; i--) {
if (num >= (1 << i)) {
out[15-i] = '1';
num -= (1 << i);
} else {
out[15-i] = '0';
}
}
if (argv[1][0] == '-') {
for (int i = 0; i < 16; i++) {
if (out[i] == '0') {
out[i] = '1';
} else {
out[i] = '0';
}
}
for (int i = 15; i >= 0; i--) {
if (out[i] == '1') {
out[i] = '0';
} else {
out[i] = '1';
break;
}
}
}
printf("%s\n", out);
int main() {
return EXIT_SUCCESS;
}