Code:
#include stdio.h
int number_between_bit_positions(int,int,int);
int result = 0;
int main()
{
int number, start_pos, end_pos;
printf("\nEnter the number");
scanf("%d", &number);
printf("\nEnter the position of a and b");
scanf("%d %d", &start_pos, &end_pos);
result = number_between_bit_positions(number, start_pos, end_pos);
printf("Byte Equivalent of bits between %d and %d positions %d", start_pos, end_pos, result);
}
int number_between_bit_positions(int number, int start_pos , int end_pos)
{
int i, j, shift_num, res_val;
/*
* Right shift to the specified start position,take the corresponding bits using &
* Left shift to locate the bits in their respective positions
*/
for (i = start_pos, j = 0;i <= end_pos;i++,j++)
{
shift_num = number >> i;
res_val = shift_num & 1;
res_val = res_val << j;
result += res_val;
}
return result;
}
Output:
Enter the number78
Enter the position of a and b
3
6
Byte Equivalent of bits between 3 and 6 positions is 9
More C Programs: