summaryrefslogtreecommitdiffstats
path: root/2
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2021-11-30 19:41:19 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2021-11-30 19:41:19 +0800
commit025189ebf4a9543bc0d1f9c8dd880762f0d4a2ed (patch)
tree6d2d80237e19afa9e191a930d45a67eff99910b1 /2
parent1c86ec65de96f50a3f7b38c08e3bffe1dd81083f (diff)
downloadk&r-exercises-025189ebf4a9543bc0d1f9c8dd880762f0d4a2ed.tar.gz
2.7
Diffstat (limited to '2')
-rw-r--r--2/7.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/2/7.c b/2/7.c
new file mode 100644
index 0000000..e35ec2e
--- /dev/null
+++ b/2/7.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+/* inverts n bits of x starting at p */
+unsigned invert(unsigned x, int p, int n);
+
+int main(int argc, char * argv[]) {
+ return 0;
+}
+
+unsigned invert(unsigned x, int p, int n) {
+ unsigned xUnset, xSet, xInv;
+
+ // mask to unset n bits starting at p:
+ xUnset = (~0 << p) | ~(~0 << (p - n));
+
+ // mask to extract bits to invert:
+ xSet = ~xUnset;
+
+ // mask out x and copy inverted bits into unset bits:
+ xInv = (x & xUnset) | ~(x & xSet);
+
+ return xInv;
+} \ No newline at end of file