summaryrefslogtreecommitdiff
path: root/19_bits_arr
diff options
context:
space:
mode:
Diffstat (limited to '19_bits_arr')
-rw-r--r--19_bits_arr/.gitignore1
-rw-r--r--19_bits_arr/Makefile2
-rw-r--r--19_bits_arr/README47
-rw-r--r--19_bits_arr/bits_ans.txt10
-rw-r--r--19_bits_arr/grade.txt19
-rw-r--r--19_bits_arr/numToBits.c70
6 files changed, 149 insertions, 0 deletions
diff --git a/19_bits_arr/.gitignore b/19_bits_arr/.gitignore
new file mode 100644
index 0000000..1be726c
--- /dev/null
+++ b/19_bits_arr/.gitignore
@@ -0,0 +1 @@
+numToBits
diff --git a/19_bits_arr/Makefile b/19_bits_arr/Makefile
new file mode 100644
index 0000000..b0fe89a
--- /dev/null
+++ b/19_bits_arr/Makefile
@@ -0,0 +1,2 @@
+numToBits: numToBits.c
+ gcc -o numToBits -pedantic -std=gnu99 -Wall -Werror numToBits.c
diff --git a/19_bits_arr/README b/19_bits_arr/README
new file mode 100644
index 0000000..019d6cf
--- /dev/null
+++ b/19_bits_arr/README
@@ -0,0 +1,47 @@
+For this problem, you will be splitting numbers (a 32 bit integer)
+up into their individual bits. I have provided a function for you
+(getNthBit) which will return a specific bit from a number. For example,
+getNthBit(x,0) would return the first (0th) bit, getNthBit(x,1) would
+return the next bit, and so on.
+
+While we normally use "int" for our numbers, we are using "uint32_t".
+This is just like "int" except that they are unsigned ints which are
+guaranteed to be 32 bits long (by contrast, the normal "int" type
+is signed, and there are no guarantees as to how many bits are in it).
+We are doing this to be precise and correct: you can use a uint32_t
+in much the same way as an int.
+
+1. Open the file numToBits.c
+ Find the function
+
+void numToBits(uint32_t * nums, int nNums, int * bits, int nBits) ;
+
+ This function takes in two arrays: nums (of length nNums), and
+ bits (of length nBits). This function should:
+ - Check that there is enough space in bits to hold all the bits
+ of "nums". Note that each number in "nums" will results in 32
+ bits in "bits". If this is not true, your function should
+ print a message with the format:
+ "Invalid call to numToBits! nBits is %d, nNums is %d\n",
+ (where the first %d is nBits, and the second %d is nNums)
+ then return without doing anything else.
+
+ - Put the individual bits of each number into the "bits" array.
+ The bits put into this array should be ordered so that the first
+ 32 bits represent nums[0], the next 32 bits are nums[1], and so
+ on. Within each number, the most significant bit (bit 31) should
+ come first, and the least significant bit (bit 0) should come last.
+ That is, bits[0] should be bit 31 of nums[0], bits[1] should
+ be bit 30 of nums[0], and so on.
+
+
+
+ 2. Compile and test your code.
+ We have provided a main function which will print test cases
+ and your function's answer for them. We have provided the
+ correct output in bits_ans.txt
+
+ 3. Submit as usual
+
+
+
diff --git a/19_bits_arr/bits_ans.txt b/19_bits_arr/bits_ans.txt
new file mode 100644
index 0000000..9702a22
--- /dev/null
+++ b/19_bits_arr/bits_ans.txt
@@ -0,0 +1,10 @@
+ 1 ( 1) => 00000000000000000000000000000001
+ 2 ( 2) => 00000000000000000000000000000010
+ 3 ( 3) => 00000000000000000000000000000011
+ 4 ( 4) => 00000000000000000000000000000100
+ 5 ( 5) => 00000000000000000000000000000101
+ 15 ( F) => 00000000000000000000000000001111
+ 109 ( 6D) => 00000000000000000000000001101101
+ 123456789 ( 75BCD15) => 00000111010110111100110100010101
+ 987654321 (3ADE68B1) => 00111010110111100110100010110001
+Invalid call to numToBits! nBits is 223, nNums is 7
diff --git a/19_bits_arr/grade.txt b/19_bits_arr/grade.txt
new file mode 100644
index 0000000..54495d6
--- /dev/null
+++ b/19_bits_arr/grade.txt
@@ -0,0 +1,19 @@
+Grading at Wed 20 Oct 2021 02:04:01 AM UTC
+Attempting to compile numToBits.c
+Your file matched the expected output
+Your output matched what we expected
+Removing your main() and replacing it with our own to run more tests...
+#################################################
+testcase2:
+array size:0 was Correct
+#################################################
+testcase3:
+array size:1 was Correct
+#################################################
+testcase4:
+array size:100 was Correct
+#################################################
+testcase5:
+array size:5000 was Correct
+
+Overall Grade: A
diff --git a/19_bits_arr/numToBits.c b/19_bits_arr/numToBits.c
new file mode 100644
index 0000000..c7538da
--- /dev/null
+++ b/19_bits_arr/numToBits.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+/*
+ * Helper function that retrieves numbers 'bit' value
+ * ie: 1's 31st bit is 1
+ */
+int getNthBit(uint32_t number, int bit) {
+ if (bit <0 || bit >= 32) {
+ printf("Bit %d is invalid\n", bit);
+ exit (EXIT_FAILURE);
+ }
+ return (number & (1<<bit)) != 0;
+}
+
+/*
+ *
+ * This function takes in two arrays: nums (of length nNums), and
+ * bits (of length nBits). This function should:
+ *
+ * - Check that there is enough space in bits to hold all the bits
+ * of "nums". Note that each number in "nums" will results in 32
+ * bits in "bits". If this is not true, your function should
+ * print a message with the format:
+ * "Invalid call to numToBits! nBits is %d, nNums is %d\n",
+ * (where the first %d is nBits, and the second %d is nNums)
+ * then return without doing anything else.
+ *
+ * - Put the individual bits of each number into the "bits" array.
+ * The bits put into this array should be ordered so that the first
+ * 32 bits represent nums[0], the next 32 bits are nums[1], and so
+ * on. Within each number, the most significant bit (bit 31) should
+ * come first, and the least significant bit (bit 0) should come last.
+ * That is, bits[0] should be bit 31 of nums[0], bits[1] should
+ * be bit 30 of nums[0], and so on.
+ */
+void numToBits(uint32_t * nums, int nNums, int * bits, int nBits) {
+ if (nBits != nNums * 32) {
+ printf("Invalid call to numToBits! nBits is %d, nNums is %d\n", nBits, nNums);
+ return;
+ }
+ for (int i = 0; i < nNums; i++) {
+ for (int j = 0; j < 32; j++) {
+ bits[i*32 + j] = getNthBit(nums[i], 31 - j);
+ }
+ }
+}
+
+void doTest(uint32_t * nums, int n) {
+ int bits[n *32];
+ numToBits(nums, n, bits, n*32);
+ for (int i =0; i < n; i++) {
+ printf(" %9d (%8X) => ", nums[i], nums[i]);
+ for (int j = 0; j < 32; j++) {
+ printf("%d", bits[i*32 + j]);
+ }
+ printf("\n");
+ }
+}
+
+int main(void) {
+ uint32_t array1[] = { 1, 2, 3, 4, 5, 15, 109};
+ uint32_t array2[] = { 123456789, 987654321 };
+ int bits[7*32-1];
+ doTest (array1, 7);
+ doTest (array2, 2);
+ numToBits(array1,7, bits , 7*32-1);
+ return EXIT_SUCCESS;
+}