Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81568 / c2html / test.c
diff options
context:
space:
mode:
Commit06811744a002f4ab332d94d510475231afcd45a1 (patch)
Author
 Mincho Paskalev <minchopaskal@gmail.com>
2018-11-03 13:36:57 +0200
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2018-11-03 13:36:57 +0200
Files91c41776fbdf899dcb61a8d5df3ac1316c24f3c8 /homework/81568/c2html/test.c
IPL-06811744a002f4ab332d94d510475231afcd45a1.tar
IPL-06811744a002f4ab332d94d510475231afcd45a1.tar.gz
IPL-06811744a002f4ab332d94d510475231afcd45a1.zip
Parent26e5f891d537b5d4fc2890c9cdbeb73d5748d568 (diff)
First HW (#9)
* Add 1st hw * --amend * moving readme for 1st hw to designated folder * renamed TokenType enum to TokenClass * fixed character constant lexing. added sampe output file * add new line at end-of-file. added gitignore * a bit of refactoring
1 files changed, 108 insertions, 0 deletions
-rw-r--r--homework/81568/c2html/test.c108
diff --git a/homework/81568/c2html/test.c b/homework/81568/c2html/test.c
new file mode 100644
index 0000000..636032a
--- /dev/null
+++ b/homework/81568/c2html/test.c
@@ -0,0 +1,108 @@
+#ifndef SAMPLE_CODE_C
+#define SAMPLE_CODE_C
+
+#define ONE_LINE_MACRO 5
+
+#include <stdio.h>
+
+int bitcount(long long x) {
+ int cnt = 0;
+ while (x) {
+ ++cnt;
+ x &= (x-1);
+ }
+ return cnt;
+}
+
+void shift_to_pos(int *num, int p, int n) {
+ *num = p > n ? *num << (p - n + 1) : *num >> (n - p + 1);
+}
+
+int setbits(x, p, n, y) {
+ int mask = (1 << n) - 1;
+ int to_set = y & mask;
+ shift_to_pos(&mask, p, n);
+ shift_to_pos(&to_set, p, n);
+ return (x & ~mask) | to_set;
+}
+
+unsigned invert(unsigned x, unsigned p, unsigned n) {
+ ++p; // in case p < n
+ while(n-- && p--) {
+ if (!(x & (1 << n))) {
+ x |= (1 << n);
+ } else {
+ x ^= (1 << n);
+ }
+ }
+ return x;
+}
+
+int rightrot(long long x, int n) {
+ int bitcnt = bitcount(x);
+ x <<= n;
+ return (x & ( (1 << (bitcnt)) - 1) ) | (x & ~( (1 << bitcnt) - 1 )) >> bitcnt;
+}
+
+char num[32];
+
+void to_binary(unsigned x) {
+ int i = 31;
+ num[32] = '\0';
+ while (x) {
+ if (x & 1) num[i] = '1';
+ else num[i] = '0';
+ --i;
+ x >>= 1;
+ }
+ for (; i >= 0; --i) num[i] = '0';
+ for (; i < 32; ++i) {
+ printf("%c%s", num[i], !((i+1) % 4) ? " " : "");
+ }
+}
+
+void switch_why_not(void) {
+ char c = 'X';
+ switch(c) {
+ case 'x':
+ printf("hex\n");
+ break;
+ case 'X':
+ printf("why break stuff\n");
+ default:
+ break;
+ }
+
+ return;
+}
+
+main() {
+ long long x = 0b110110;
+ printf("rightrot:\n");
+ to_binary(x);
+ printf("\n");
+ to_binary(rightrot(x, 3));
+ printf("\n");
+
+ unsigned i = 0b101010;
+ printf("invert:\n");
+ to_binary(i);
+ printf("\n");
+ to_binary(invert(i, 4, 4));
+ printf("\n");
+
+ long long a = 0b111001010011;
+ long long b = 0b11111010;
+ printf("bits of: "); to_binary(a); printf("\n");
+ printf("bits to: "); to_binary(b); printf("\n");
+ to_binary(setbits(a, 6, 4, b));
+
+
+ int testInt = 12345678;
+ float testFloat = 1234.5678f;
+ double testDouble = 1234.5678;
+
+ return 0;
+}
+
+#endif /* SAMPLE_CODE_C */