Please write answer in c!
Suppose you have an unsigned int variable (4 bytes; 32bits). If a user intends to store a single unsigned value in thatvariable, then the minimum and maximum that value can be are 0 and232 − 1 = 4294967295, respectively. Now suppose a userwants to store k unsigned integer values in that variablewhere 32 is divisible by k. Here are some questions toconsider:
• Assuming each value uses the same number of bits, howmany bits are available for each value?• Assuming each value has the same range, what is the minimumand maximum that each value can take on? Note:• Do not use arrays or strings to solve this problem. Youdon’t need them.• You can use extra variables, loops, and operators asneeded.• 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 valuesin 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 bypartitioning 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 bypartitioning 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 bypartitioning 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 valuesin x by partitioning the space into 32 groups of 1 biteach. Then each of the 32 values can take on a minimum and maximumof 0 and 1, respectively.
Specific Examples
Assume you have unsigned int x. In the examples below,spaces are provided for readability only. The “Values” rowcorresponds to the base-10 representations of the binary digitswithin 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 20are all stored using a single unsigned int x variable,respectively. When x is printed as a single 32-bitunsigned integer, its overall value should be 52562708.
Bits: 0000 0011 0010 0010 0000 1011 00010100 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 intx variable,
respectively. When x is printed as a single 32-bitunsigned integer, its overall value should be 255395456.
Bits: 0000 1111 0011 1001 0000 0110 10000000 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 needto do the following:
Prompt the user to enter a value for k.
Prompt the user to enter k unsigned integers. Theintegers are to be entered in a single line separated by spaces.Place the k integers into the unsigned intx using bitwise operators.
(a) The first integer should occupy the leftmost bitsof x, and the last integer should occupy the rightmost bitsof x.
(b) If one of the k integers is too large to fitinto one of the k groups of bits, then an error messageshould be displayed
and the program should terminate.
Display the overall value of x and terminate theprogram.
Sample Inputs and Outputs
Here are some sample inputs and outputs. Your program shouldmimic such behaviors:
$ Please enter k: 8 $Pleaseenter8unsignedints: 015390680 $Overall Value = 255395456
$ Please enter k: 8 $Pleaseenter8unsignedints: 0163906180 $ Theinteger 16 is an invalid input.
Please note that the last example illustrates a scenario inwhich an input integer is too large. Since k is 8, the 32bits are divided into 8 groups, each consisting of 4 bits. Thelargest unsigned integer that can be represented using 4 bits is 15(binary representation 1111), so 16 cannot fit into 4 bits and isan invalid input. Also note that later on another input, 18, isalso invalid, but your program just needs to display the errormessage in reference to the first invalid input and terminate.
Please write answer in c! Suppose you have an unsigned int variable (4 bytes; 32 bits). If a user intends to store a sin
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am