Question: In C language: The program deals with the array, which is consist of several bytes. Your task is to implement functions that manipulate individual bits
In C language:
The program deals with the array, which is consist of several bytes. Your task is to implement functions that manipulate individual bits in the array. Please read Notes below before starting implementation.
Your task is to implement the following functions:
a) Basic operations
In the following functions, data parameter indicates start of the array and i parameter denotes position of the bit in the input array.
Implement the following functions which manipulates bit of input array:
-
void op_bit_set(unsigned char* data, int i) sets a bit in input data.
-
void op_bit_unset(unsigned char* data, int i) resets a bit in input data.
-
int op_bit_get(const unsigned char* data, int i) returns value 0, if bit value in i is zero and returns value 1, if bit value in i is one.
b) Print a byte
Implement function void op_print_byte(unsigned char b), which prints one unsigned chars binary representation.
c) Get a sequence
Implement function unsigned char op_bit_get_sequence(const unsigned char* data, int i, int how_many), that separates a maximum of 8 bits long binary number from the array and returns it. i and data have same meaning as above. how_many indicates how many bits need to be counted from the i (max. 8). If how_many is less than 8, the most significant bits of the returned number is left with zeros. In this task, you may want to take advantage of function implemented in (a).
Notes:
-
In this task, bit 0 is the most significant bit. It is also assumed that unsigned char is exactly 8 bits (1 byte). Thus, for example, bit 8 is a leftmost bit in second unsigned char byte and bit 17 is the second highest bit in the third unsigned char byte. Thus, examining the number 170 (0xAA hexadecimal format, 10101010 in binary), the most significant bit, ie bit 0 has a value of 1.
-
If you find yourself implementing some sort of helper array of characters or integers, then you are doing something wrong.
-
Bit 5 from an array is not array[5].
-
If given binary data is 1110 0101 1111 0011 0001 1110 0100 1111, and the op_bit_get_sequence function is called with index 20, and how_many of 5, return value should be 28, i.e. 5 first bits of the number 1110 0100 1111 from the 20th index i.e. 1 1100. The return value should then be 0001 1100 ( 0001 1100 = 28).
Some hints from the file:
/* NOTE: * ----------- * The parameter binary data (const unsigned char*) in all the functions * below is likely to hold more than 8 bits. You should take this into * account when implementing your functions. */
/* DESCRIPTION: * ------------ * The function sets a bit with index i as active (1) in the parameter * binary data. * * You do not need to handle any invalid parameters. * * PARAMETERS: * ------------ * unsigned char* data: an array of binary data. * int i: the index of the bit which to set as active (1). * * RETURNS: * ------------ * Nothing. * */ void op_bit_set(unsigned char* data, int i) { }
/* DESCRIPTION: * ------------ * The function sets a bit with index i as inactive (0) in the parameter * binary data. * * You do not need to handle any invalid parameters. * * PARAMETERS: * ------------ * unsigned char* data: an array of binary data. * int i: the index of the bit which to set as active (1). * * RETURNS: * ------------ * Nothing. * */
void op_bit_unset(unsigned char* data, int i) { }
/* DESCRIPTION: * ------------ * The functions returns 1 or 0 depending on whether the bit with index i in * the binary data is set or not. * * You do not need to handle any invalid parameters. * * PARAMETERS: * ------------ * const unsigned char* data: an array of binary data. * int i: the index of the bit which value to return. * * RETURNS: * ------------ * 0: if the bit was not set. * 1: if the bit was set. */
int op_bit_get(const unsigned char* data, int i) { }
/* DESCRIPTION: * ------------ * The function prints the binary representation of the parameter unsigned char * to the standard output as 1s and 0s. Note that one unsigned char is 8 bits. * There is no newline at the end of the print. * * For example: * * unsigned char b = 0xF8; * print_byte(b); * * Would print out: * * 11111000 * * PARAMETERS: * ------------ * unsigned char b: the binary sequence. * * RETURNS: * ------------ * Nothing. * */
void op_print_byte(unsigned char b) { }
/* DESCRIPTION: * ------------ * The function extracts a sequence of bits from the binary data. The length of * the sequence is defined in by the parameter how_many and the sequence starts * from index i (inclusive). * * If the parameter how_many is less than 8, the byte will be padded with zeroes * on the left side. For example if you extract 2 bits (11), the return value * is 00000011, which is equal to 3. * * The maximum length of extracted sequence for this function is 8, you do not * need to handle any invalid parameters. * * NOTE: The const unsigned char* data is likely to hold more than one byte of * information. The extracted sequence also can continue over byte * boundaries i.e. the extracted bit data can exist within two different * bytes. * * PARAMETERS: * ------------ * const unsigned char* data: an array of binary data, where the sequence is * extracted from. * * int i: the starting index of the extracted sequence. * int how_many: the length of the extracted sequence. * * RETURNS: * ------------ * The extracted sequence stored in a single unsigned char. * */
unsigned char op_bit_get_sequence(const unsigned char* data, int i, int how_many) { }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
