mirror of
https://github.com/Comp211-SP24/lab-02-Rushilwiz.git
synced 2025-04-21 12:30:18 -04:00
finished signed and twos
This commit is contained in:
parent
2af160daeb
commit
9114a8bda9
29
signed.c
29
signed.c
|
@ -1,8 +1,33 @@
|
||||||
// PID: 9DigitPidNoSpacesOrDashes
|
// PID: 730677144
|
||||||
// I pledge the COMP 211 honor code.
|
// I pledge the COMP 211 honor code.
|
||||||
|
|
||||||
#include <stdlib.h>
|
#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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
44
twos.c
44
twos.c
|
@ -1,8 +1,48 @@
|
||||||
// PID: 9DigitPidNoSpacesOrDashes
|
// PID: 730677144
|
||||||
// I pledge the COMP 211 honor code.
|
// I pledge the COMP 211 honor code.
|
||||||
|
|
||||||
#include <stdlib.h>
|
#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;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user