$24
Problem 1
Design and implement bit operations on a single unsigned int word as a bit vector:
Let N be sizeof(unsigned int). Then we can have a bit vector of size N represented by an integer word.
Set operation: This operation sets the jth bit (1<=j<=N) (counting 0 as the LSB) of the given integer word S to 1. (j is taken as input)
Get operation. This operation returns the jth bit (1<=j<=N) (counting 0 as the LSB) of the given integer word S. (j is taken as input)
Obtaining jth bit of an int in C language can be achieved by masking the vector with a bit pattern and testing it: i.e. (S & Bj) iff (jth bit of S is 1) where Bj is all 0s except the jth bit. Bj can be obtained by left-shifting ((unsigned int) 1) j times.
Implement the set and get operations on an unsigned int, to be used as a bit vector. You can use the following table for designing your functions.
Key
Function
Input Format
Description
0
CreateBitVector
0
Creates a bit vector S in the form of an integer word of size –
sizeof(unsigned int), which is equal to 32; And then initializes
it to 0.
1
Set
1 j
Sets the value of jth bit of S to 1.
2
Get
2 j
Returns the value of jth bit of S. You must also print the
returned value.
Sample input and output
Sample Input
Sample Output
0
1
1 2
1
1 6
0
9
6
9
31 -1
1 | P a g e