I've written C code for setting, clearing, and toggling individual bits in an integer
However, I now want to expand upon this and create a more versatile program that can set, clear, or toggle a specified number of bits within an integer value. To illustrate, let me provide you with an example:
Suppose the user inputs the following instructions:
I would like to develop a program that can take such user-defined instructions and perform the corresponding bit manipulations on the integer value. looking your help
C:
#include<stdio.h>
void Bit_Set ( int num, int pos)
{
num = num | 1 << pos;
printf(" num = %d", num);
}
void Bit_Clear ( int num, int pos)
{
num = num & (~( 1 << pos));
printf(" num = %d", num);
}
void Bit_toggle ( int num, int pos)
{
num = num ^ 1 << pos;
printf(" num = %d", num);
}
int main()
{
//Bit_Set ( 0, 2);
//Bit_Clear ( 255, 1);
//Bit_toggle ( 255, 7);
return 0;
}
Suppose the user inputs the following instructions:
- Set the higher two bits of the integer.
- Clear three bits from the upper next, higher- bytes of the integer.
- Toggle three bits in the lower-order bytes of the integer.
I would like to develop a program that can take such user-defined instructions and perform the corresponding bit manipulations on the integer value. looking your help