aboutsummaryrefslogtreecommitdiff
path: root/2.6.c
diff options
context:
space:
mode:
authorsinanmohd <pcmsinan@gmail.com>2022-06-04 12:11:15 +0530
committersinanmohd <pcmsinan@gmail.com>2022-06-04 12:11:15 +0530
commitc24973af02bc33f2f5f25d37e22ca91da5de3c47 (patch)
tree460a005b6a3a9a967bd881bc2bf01e0becbe33cc /2.6.c
inital commit
Diffstat (limited to '2.6.c')
-rw-r--r--2.6.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/2.6.c b/2.6.c
new file mode 100644
index 0000000..23ce1e6
--- /dev/null
+++ b/2.6.c
@@ -0,0 +1,47 @@
+#include <stdio.h>
+
+#define INT_NBITS 32
+
+unsigned getbits(unsigned x, int from, int to);
+void pbits(int x);
+unsigned setbits(unsigned x, int from, int to, int source);
+
+int
+main(void)
+{
+ int x = 423834583;
+ int y = 354543434;
+
+ pbits(x);
+ pbits(y);
+
+ pbits(setbits(x, 12, 5, y));
+
+ return 0;
+}
+
+unsigned
+getbits(unsigned x, int from, int to)
+{
+ return (x >> (from+1-to)) & ~(~(unsigned)0 << to);
+}
+
+void
+pbits(int x)
+{
+ for (int i = INT_NBITS-1; i >= 0; i--) {
+ if (i%4 == 3)
+ printf(" ");
+
+ printf("%u", getbits(x, i, 1));
+ }
+
+ printf("\n");
+}
+
+unsigned
+setbits(unsigned x, int from, int to, int source)
+{
+ int model = ((((unsigned)~0 << (INT_NBITS-from)) >> (INT_NBITS - (from+to))) << to);
+ return (x & ~model) | (source & model);
+}