summaryrefslogtreecommitdiff
path: root/2/3_int_arith
diff options
context:
space:
mode:
Diffstat (limited to '2/3_int_arith')
-rw-r--r--2/3_int_arith/212.c36
-rw-r--r--2/3_int_arith/225.c20
-rw-r--r--2/3_int_arith/227.c20
-rw-r--r--2/3_int_arith/230.c25
-rw-r--r--2/3_int_arith/236.c19
-rw-r--r--2/3_int_arith/242.c14
-rw-r--r--2/3_int_arith/243.c22
-rwxr-xr-x2/3_int_arith/checkbin0 -> 15688 bytes
-rw-r--r--2/3_int_arith/check-211.c27
-rw-r--r--2/3_int_arith/show-bytes.c33
-rw-r--r--2/3_int_arith/xdr.c28
11 files changed, 244 insertions, 0 deletions
diff --git a/2/3_int_arith/212.c b/2/3_int_arith/212.c
new file mode 100644
index 0000000..e746216
--- /dev/null
+++ b/2/3_int_arith/212.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+
+int bis(int x, int m) {
+ return x | m;
+}
+int bic(int x, int m) {
+ return x & ~m;
+}
+int bool_or(int x, int y) {
+ int result = bis(x, y);
+ return result;
+}
+int bool_xor(int x, int y) {
+ int result = bis(bic(x, y), bic(y, x));
+ return result;
+}
+int bool_eq(int x, int y) {
+ int result = !(x^y);
+ return result;
+}
+
+void main() {
+ printf("=== 2.11 ===\n");
+ int x = 0x87654321;
+ printf("%x\n", x & 0xFF);
+ printf("%x\n", ~x ^ 0xFF);
+ printf("%x\n", x | 0xFF);
+
+ printf("=== 2.12 ===\n");
+ printf("%x\n", bool_or(1, 1));
+ printf("%x\n", bool_xor(1, 1));
+
+ printf("=== 2.15 ===\n");
+ printf("%x\n", bool_eq(0x87654321, 0x87654321));
+ printf("%x\n", bool_eq(14, 13));
+}
diff --git a/2/3_int_arith/225.c b/2/3_int_arith/225.c
new file mode 100644
index 0000000..51f1937
--- /dev/null
+++ b/2/3_int_arith/225.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <limits.h>
+
+float sum_elements(float a[], unsigned length) {
+ int i;
+ float result = 0;
+
+ for (i = 0; i <= length-1; i++) {
+ printf("iter\n");
+ result += a[i];
+ }
+ return result;
+}
+
+void main() {
+ printf("hi\n");
+ float ele[] = {1.0, 2.0};
+ // sum_elements(ele, 0);
+ printf("%d", INT_MAX+1);
+}
diff --git a/2/3_int_arith/227.c b/2/3_int_arith/227.c
new file mode 100644
index 0000000..cc22cf9
--- /dev/null
+++ b/2/3_int_arith/227.c
@@ -0,0 +1,20 @@
+#include <stdio.h>
+#include <limits.h>
+
+// with limits.h
+int uadd_ok(unsigned x, unsigned y) {
+ printf("x: %u, y: %u\n", x, y);
+ return x <= (UINT_MAX - y);
+}
+
+// without limits.h
+int uadd_ok_without(unsigned x, unsigned y) {
+ unsigned sum = x + y;
+ return sum >= x;
+}
+
+void main() {
+ printf("%u\n", UINT_MAX);
+ printf("%d\n", uadd_ok(10, 20));
+ printf("%d", uadd_ok(UINT_MAX / 2, UINT_MAX / 2));
+}
diff --git a/2/3_int_arith/230.c b/2/3_int_arith/230.c
new file mode 100644
index 0000000..8bba282
--- /dev/null
+++ b/2/3_int_arith/230.c
@@ -0,0 +1,25 @@
+#include <limits.h>
+#include <stdio.h>
+
+int tadd_ok(int x, int y) {
+ int positive_overflow = x > 0 && y > 0 && x + y < 0;
+ int negative_overflow = x < 0 && y < 0 && x + y >= 0;
+ return !positive_overflow && !negative_overflow;
+}
+
+int tadd_buggy(int x, int y) {
+ int sum = x+y;
+ printf("sum: %d, sum-x: %d, sum-y: %d", sum, sum-x, sum-y);
+ return (sum-x == y) && (sum-y == x);
+}
+
+/* buggy */
+int tsub_ok(int x, int y) {
+ return tadd_ok(x, -y);
+}
+
+void main() {
+ printf("INT_MIN: %d, %d\n", -(INT_MIN), INT_MIN);
+ printf("result: %d\n", tadd_ok(10, 100));
+ printf("result2: %d", tadd_buggy(INT_MAX, 1)); // The sum is a very negative number, when we take the difference with it's parts it just does a negative overflow to get the original parts back.
+}
diff --git a/2/3_int_arith/236.c b/2/3_int_arith/236.c
new file mode 100644
index 0000000..b05da34
--- /dev/null
+++ b/2/3_int_arith/236.c
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+int tmult_ok(int x, int y) {
+ int64_t upcasted_result = (int64_t) x * y;
+ return upcasted_result == (int) upcasted_result;
+}
+
+void main() {
+ int m = 1<<31;
+ int n = 1<<31;
+ printf("%" PRId64 "\n", (int64_t) (m * n));
+ printf("%d\n", tmult_ok(m, n));
+ printf("%d\n", tmult_ok(10, 20));
+ printf("%d\n", tmult_ok(10, 20));
+}
+
+
diff --git a/2/3_int_arith/242.c b/2/3_int_arith/242.c
new file mode 100644
index 0000000..07634e3
--- /dev/null
+++ b/2/3_int_arith/242.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int div16(int x) {
+ // my inefficient method that just checks the sign bit: int sign_bit = (unsigned) (x & 1<<31) >> 31;
+ int bias = (x >> 31) & 0xF;
+ return (x + bias)>>4;
+}
+
+void main() {
+ int result = div16(-17);
+ printf("%d\n", result);
+ result = div16(17);
+ printf("%d\n", result);
+}
diff --git a/2/3_int_arith/243.c b/2/3_int_arith/243.c
new file mode 100644
index 0000000..9a60eda
--- /dev/null
+++ b/2/3_int_arith/243.c
@@ -0,0 +1,22 @@
+// #define M
+// #define K
+// int arith(int x, int y) {
+// int result = 0;
+// result = x*M + y/N;
+// return result;
+// }
+//
+// int optarith(int x, int y) {
+// int t = x;
+// x <<= 5;
+// x -= t;
+// if (y < 0) y += 7;
+// y >>= 3;
+// return x+y;
+// }
+#include <stdio.h>
+
+int main() {
+ int x = 3;
+ printf("%d", x<<1);
+}
diff --git a/2/3_int_arith/check b/2/3_int_arith/check
new file mode 100755
index 0000000..f50a240
--- /dev/null
+++ b/2/3_int_arith/check
Binary files differ
diff --git a/2/3_int_arith/check-211.c b/2/3_int_arith/check-211.c
new file mode 100644
index 0000000..174b0f4
--- /dev/null
+++ b/2/3_int_arith/check-211.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+
+void inplace_swap(int *x, int *y) {
+ *y = *x ^ *y;
+ *x = *x ^ *y;
+ *y = *x ^ *y;
+}
+
+void reverse_array(int a[], int cnt) {
+ int first, last;
+ for (first = 0, last = cnt-1;
+ first < last;
+ first++,last--)
+ inplace_swap(&a[first], &a[last]);
+}
+
+void print_array(int a[], int len) {
+ int i;
+ for (i = 0; i < len; i++)
+ printf("%d", a[i]);
+}
+
+void main() {
+ int a[] = {1,2,3,4,5, 6};
+ reverse_array(a, 6);
+ print_array(a, 6);
+}
diff --git a/2/3_int_arith/show-bytes.c b/2/3_int_arith/show-bytes.c
new file mode 100644
index 0000000..0860b8d
--- /dev/null
+++ b/2/3_int_arith/show-bytes.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+
+typedef unsigned char *byte_pointer;
+
+void show_bytes(byte_pointer start, size_t len) {
+ int i;
+ for (i = 0; i < len; i++)
+ printf(" %.2x", start[i]);
+ printf("\n");
+}
+
+void show_int(int x) {
+ show_bytes((byte_pointer) &x, sizeof(int));
+}
+
+
+void show_float(float x) {
+ show_bytes((byte_pointer) &x, sizeof(float));
+}
+
+
+void show_pointer(void *x) {
+ show_bytes((byte_pointer) &x, sizeof(void *));
+}
+
+void test_show_bytes(int val) {
+ int ival = val;
+ float fval = (float) ival;
+ int *pval = &ival;
+ show_int(ival);
+ show_float(fval);
+ show_pointer(pval);
+}
diff --git a/2/3_int_arith/xdr.c b/2/3_int_arith/xdr.c
new file mode 100644
index 0000000..63d4b24
--- /dev/null
+++ b/2/3_int_arith/xdr.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+void* copy_elements(void* ele_src[], int ele_cnt, size_t ele_size) {
+ uint64_t asize = ele_cnt * (uint64_t) ele_size;
+ void *result = malloc(asize);
+ if (result == NULL)
+ return NULL;
+ void *next = result;
+ int i;
+ for (i = 0; i < ele_cnt; i++) {
+ memcpy(next, ele_src[i], ele_size);
+ next += ele_size;
+ }
+ return result;
+}
+
+void main() {
+ int i = 0;
+ char* input[] = {"somestring","some666666"};
+ printf("%s\n", input[0]);
+ printf("%s\n", *(input+1)); // these two lines are basically the same thing *(input+1) = input[1]
+ char* result = copy_elements((void *) input, 2, 10);
+ printf("%s\n", result);
+}