summaryrefslogtreecommitdiff
path: root/two
diff options
context:
space:
mode:
Diffstat (limited to 'two')
-rw-r--r--two/bit.c68
-rw-r--r--two/cond.c13
-rw-r--r--two/conv.c49
-rw-r--r--two/inc.c45
4 files changed, 175 insertions, 0 deletions
diff --git a/two/bit.c b/two/bit.c
new file mode 100644
index 0000000..28f0e7d
--- /dev/null
+++ b/two/bit.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+
+void
+itob(int n)
+{
+ for (int i = 15; i >= 0; --i) {
+ int j = n >> i;
+ if (j & 1) {
+ printf("1");
+ } else {
+ printf("0");
+ }
+ }
+ printf("\n");
+}
+
+/*
+ * return x with the n bits that begin at position p inverted, leaving the other
+ * bits unchanged
+ */
+void
+invert(unsigned x, int p, int n)
+{
+ int i;
+ i = 0;
+ while (n > 0) {
+ i |= 1 << p;
+ --p;
+ --n;
+ }
+ x ^= i;
+}
+
+/*
+ * return x with the n bits that begin at position p set to the rightmost n bits
+ * of y, leaving the other bits unchanged
+ */
+void
+setbits(unsigned x, int p, int n, unsigned y)
+{
+ x = x >> (p + 1 - n) & ~(~0 << n);
+ y = y & (~0 << n) | x;
+ printf("%d %d\n", x, y);
+ itob(x);
+ itob(y);
+}
+
+/* count the number of 1-bits */
+int
+bitcount(unsigned x)
+{
+ int b = 0;
+ while (x != 0) {
+ /* deletes rightmost 1-bit in x */
+ x &= (x - 1);
+ ++b;
+ }
+ return b;
+}
+
+int
+main()
+{
+ /* setbits(5, 2, 3, 16); */
+ /* invert(16, 2, 1); */
+ printf("%d\n", bitcount(4));
+ return 0;
+}
diff --git a/two/cond.c b/two/cond.c
new file mode 100644
index 0000000..5a7b88b
--- /dev/null
+++ b/two/cond.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int
+main()
+{
+ char s[] = "fOobArBaz";
+ for (int i = 0; s[i] != '\0'; ++i) {
+ (s[i] >= 'A' && s[i] <= 'Z') ? printf("%c", s[i] + 32)
+ : printf("%c", s[i]);
+ }
+ printf("\n");
+ return 0;
+}
diff --git a/two/conv.c b/two/conv.c
new file mode 100644
index 0000000..7bf218a
--- /dev/null
+++ b/two/conv.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+
+int
+lower(int c)
+{
+ if (c >= 'A' && c <= 'Z') {
+ return c + 'a' - 'A';
+ } else {
+ return c;
+ }
+}
+
+int
+atoi(char s[])
+{
+ int i, n;
+
+ n = 0;
+ for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i) {
+ n = 10 * n + (s[i] - '0');
+ }
+ return n;
+}
+
+/* convert string of hex digits into int digits */
+int
+htoi(char s[])
+{
+ int i, n;
+
+ n = 0;
+ for (i = 0; s[i] >= '0' && s[i] <= 'f'; ++i) {
+ if (s[i] >= '0' && s[i] <= '9') {
+ n = 16 * n + (s[i] - '0');
+ } else if (s[i] >= 'A' && s[i] <= 'F') {
+ n = 16 * n + (s[i] - 'A' + 10);
+ } else if (s[i] >= 'a' && s[i] <= 'f') {
+ n = 16 * n + (s[i] - 'a' + 10);
+ }
+ }
+ return n;
+}
+
+int
+main()
+{
+ printf("%d\n", htoi("1f"));
+ return 0;
+}
diff --git a/two/inc.c b/two/inc.c
new file mode 100644
index 0000000..220f025
--- /dev/null
+++ b/two/inc.c
@@ -0,0 +1,45 @@
+#include <stdio.h>
+
+/* remove s2 chars from s1 */
+void
+squeeze(char s1[], char s2[])
+{
+ int i, j, k, b;
+ for (i = j = 0; s1[i] != '\0'; ++i) {
+ b = 0;
+ for (k = 0; s2[k] != '\0'; ++k) {
+ if (s1[i] == s2[k]) {
+ b = 1;
+ }
+ }
+ if (b == 0) {
+ s1[j++] = s1[i];
+ }
+ }
+ s1[j] = '\0';
+}
+
+/* return first index in s1 where any char in s2 */
+int
+any(char s1[], char s2[])
+{
+ int i, j;
+ for (i = 0; s1[i] != '\0'; ++i) {
+ for (j = 0; s2[j] != '\0'; ++j) {
+ if (s1[i] == s2[j]) {
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
+int
+main()
+{
+ char s1[] = "foobar";
+ char s2[] = "a";
+ // squeeze(s1, s2);
+ printf("%d\n", any(s1, s2));
+ return 0;
+}