Question: 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
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.
For any valid value of k each of the 0 through k 1 indices denotes one of the k groups of bits within the variable x. Although the choice of having the indices start from the right is entirely arbitrary, it does make some of the math a little easier to work out.
k = 1: You can store a single unsigned values in x by using all 32 bits. Indices: 0 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. Indices: 1 0 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. Indices: 3 2 1 0 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. Indices: 7 6 5 4 3 2 1 0 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.
Specific Examples
Assume you have unsigned int x. In the examples below, spaces are provided for readability only. For any valid value of k each of the 0 through k 1 indices denotes one of the k groups of bits within the variable x. The values correspond to the base-10 representation of the binary digits within a particular group.
k = 4: In the example below, the values 20, 11, 34, and 3 are all stored using a single unsigned int variable, respectively. Indices: 3 2 1 0 Bits: 0000 0011 0010 0010 0000 1011 0001 0100 Values: 3 34 11 20
k = 8: In the example below, the values 0, 8, 6, 0, 9, 3, 15, and 0 are all stored using a single unsigned int variable, respectively. Indices: 7 6 5 4 3 2 1 0 Bits: 0000 1111 0011 1001 0000 0110 1000 0000 Values: 0 15 3 9 0 6 8 0 2
Code Description
For the code writing portion of this breakout/lab, you will need to write two functions: 1
void setValue(unsigned int & var, unsigned int k, unsigned int i, unsigned int val) This function takes in our storage variable by reference, the number of values k, the index of a value to set (0 through k 1), and a value to set. You may assume that all inputs are valid. For example, assuming your unsigned int storage variable is x, in order to use the whole variable to store a value, you should be able to type something like setValue(x, 1, 0, 1337). If you want to to be able to store 32 values (one value for each bit) and only assign the second bit to 1, then you should be able to type something like setValue(x, 32, 1, 1).
NOTE: Do NOT assume that successive calls to setValue increment the index by 1. Your function should be able to assign values to index positions in an arbitrary order.
NOTE: Do NOT assume that the group of bits your are setting are already 0s. Your function should be able to assign values to the same index multiple times.
NOTE: Since the variable is passed in by reference, changing its value inside of the function will result in its value being changed outside of the function as well. See DEITEL 6.13.
unsigned int getValue(unsigned int var, unsigned int k, unsigned int i)
This function takes in our storage variable by value, the number of values k, and the index of a of a value to get (0 through k1). You may assume that all inputs are valid. For example, if x is the unsigned int variable corresponding to the first example in the Specific Examples section, then getValue(x, 4, 0) would return 20 and getValue(x, 4, 2) would return 34.
main.cpp
#include
#include "lab.h"
using namespace std;
int main() {
// you will need variable declarations here
// ask user for a number, k -- the number of numbers to store
// loop for each value of k -- prompt user for a number and store it
// in the container value
// for () {
// }
// loop again for each value of k -- extract the value back out from
// the container variable and print it
// for () {
// }
}
lab.cpp
#include
#include "lab.h"
using namespace std;
/*
The getValue method should extract the ith (of k possible) value from
var as defined in the lab.
*/
unsigned int getValue(unsigned int var, unsigned int k, unsigned int i){
cout << "getting value from position " << i << " of " << k << endl;
return 0;
}
/*
The setValue method should set the ith (of k possible) value of var
to val. As defined in the lab.
*/
void setValue(unsigned int & var, unsigned int k, unsigned int i, unsigned int val){
cout << "setting value " << val << " into position " << i << " of " << k << " into var=" << var << endl;
}
lab.h
// lab.h -- function declarations for lab
unsigned int getValue(unsigned int var, unsigned int k, unsigned int i);
void setValue(unsigned int & var, unsigned int k, unsigned int i, unsigned int val);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
