Bitwise Operations Problem / Exercise Suppose you have an unsigned int variable (4 bytes; 32 bits). If a user intends to store a single unsigned value in that variable, then the minimum and maximum...

Please keep the constraints in mind



Bitwise Operations Problem / Exercise Suppose you have an unsigned int variable (4 bytes; 32 bits). If a user intends to store a single unsigned value in that variable, then the minimum and maximum that value can be are 0 and 232 � 1 = 4294967295, respectively. Now suppose a user wants to store k unsigned integer values in that variable where 32 is divisible by k. Here are some questions to consider: • Assuming each value uses the same number of bits, how many bits are available for each value? • Assuming each value has the same range, what is the minimum and maximum that each value can take on? Note: • Do not use arrays or strings to solve this problem. You don’t need them. • You can use extra variables, loops, and operators as needed. • You should explore using the bitwise operators. General Examples Assume you have unsigned int x. In the examples below, spaces are provided for readability only. • k = 1: You can store a single unsigned values in x by using all 32 bits. Bits: 0000 0000 0000 0000 0000 0000 0000 0000 • k = 2: You can store 2 unsigned values in x by partitioning the space into 2 groups of 16 bits each. Bits: 0000 0000 0000 0000 0000 0000 0000 0000 • k = 4: You can store 4 unsigned values in x by partitioning the space into 4 groups of 8 bits each. Bits: 0000 0000 0000 0000 0000 0000 0000 0000 • k = 8: You can store 8 unsigned values in x by partitioning the space into 8 groups of 4 bits each. Bits: 0000 0000 0000 0000 0000 0000 0000 0000 • Other valid values of k are 16 and 32. For example, when k = 32, you can store 32 unsigned values in x by partitioning the space into 32 groups of 1 bit each. Then each of the 32 values can take on a minimum and maximum of 0 and 1, respectively. 1 Specific Examples Assume you have unsigned int x. In the examples below, spaces are provided for readability only. The “Values” row corresponds to the base-10 representations of the binary digits within each of the k groups of bits. The “Overall Value” row is the base-10 representation of all 32 bits. In other words, if you were to use printf("%u", x) to print x, the “Overall Value” should be the resulting output on the screen. • k = 4: In the example below, the values 3, 34, 11, and 20 are all stored using a single unsigned int x variable, respectively. When x is printed as a single 32-bit unsigned integer, its overall value should be 52562708. Bits: 0000 0011 0010 0010 0000 1011 0001 0100 Values: 3 34 11 20 Overall Value: 52562708 • k = 8: In the example below, the values 0, 15, 3, 9, 0, 6, 8, and 0 are all stored using a single unsigned int x variable, respectively. When x is printed as a single 32-bit unsigned integer, its overall value should be 255395456. Bits: 0000 1111 0011 1001 0000 0110 1000 0000 Values: 0 15 3 9 0 6 8 0 Overall Value: 255395456 Code Description For the code writing portion of this breakout/lab, you will need to do the following: 1. Prompt the user to enter a value for k. 2. Prompt the user to enter k unsigned integers. The integers are to be entered in a single line separated by spaces. Place the k integers into the unsigned int x using bitwise operators. (a) The first integer should occupy the leftmost bits of x, and the last integer should occupy the rightmost bits of x. (b) If one of the k integers is too large to fit into one of the k groups of bits, then an error message should be displayed and the program should terminate. 3. Display the overall value of x and terminate the program. Sample Inputs and Outputs Here are some sample inputs and outputs. Your program should mimic such behaviors: $ Please enter k: 4 $ Please enter 4 unsigned ints: 3 34 11 20 $ Overall Value = 52562708 $ Please enter k: 8 $ Please enter 8 unsigned ints: 0 15 3 9 0 6 8 0 $ Overall Value = 255395456 $ Please enter k: 8 $ Please enter 8 unsigned ints: 0 16 3 9 0 6 18 0 $ The integer 16 is an invalid input. Please note that the last example illustrates a scenario in which an input integer is too large. Since k is 8, the 32 bits are divided into 8 groups, each consisting of 4 bits. The largest unsigned integer that can be represented using 4 bits is 15 (binary representation 1111), so 16 cannot fit into 4 bits and is an invalid input. Also note that later on another input, 18, is also invalid, but your program just needs to display the error message in reference to the first invalid input and terminate. 2 1 Some Nonfunctional Requirements Your submission needs to satisfy the following nonfunctional requirements: • Directory Setup: Make sure that all of your files are in a directory called LastName-FirstName-lab02, where LastName and FirstName are replaced with your actual last name and first name, respectively. For this lab, you should place your source code into a file called lab02.c. • Documentation: All functions must be documented using Javadoc (or Doxygen) style comments. Use inline docu- mentation, as needed, to explain ambiguous or tricky parts of your code. • Makefile: You need to include a Makefile. The expectation is that the grader should be able to type make clean and make to clean and compile/link your submission, respectively. The resulting executable name should be named lab02. • Standards & Flags: Make sure that when you compile, you pass the following options to gcc: -Wall -pedantic-errors Other compiler/linker options may be needed in addition to the ones mentioned above. • README: Make sure to include a README file that includes the following information presented in a reasonably formatted way: – Instructions on how to compile and run your program. Hint: the instructions should involve the make commands. • Compiler Warnings: Since you should be compiling with both the -Wall and -pedantic-errors options, your code is expected to compile without gcc issuing any warnings. 3 Some Nonfunctional Requirements Submission
Jun 15, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here